+ 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
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
+ 1
Thanks