+ 1

Suggest an algorithm to check Whether an integer array is sorted or not.

"Try to solve by creating Method" https://code.sololearn.com/c8hI0g7AZzaK/?ref=app

26th Nov 2019, 5:51 AM
Abhijeet Kumar
Abhijeet Kumar - avatar
2 Answers
+ 2
Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t!=0){ int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0; i<n; i++){ arr[i]=sc.nextInt(); } int result=arraySortedOrNot(arr,n); System.out.println(result); t--; } } I think you can simplify this part. To fill an array you only need a for loop. int n = sc.nextInt (); int [] arr = new int [n]; for (int i = 0; i < n; i++){ arr [i] = sc.nextInt (); } The algorithm itself seems okay. I would only change the if condition: if (arr [i - 1] >= arr [i]) for cases like this 1,1,1,1 or 1,2,2,4,4
26th Nov 2019, 2:25 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Thanks
26th Nov 2019, 4:21 PM
Abhijeet Kumar
Abhijeet Kumar - avatar