-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckIfArrayIsSorted.java
More file actions
46 lines (35 loc) · 1.11 KB
/
CheckIfArrayIsSorted.java
File metadata and controls
46 lines (35 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package DSA;
import java.util.Scanner;
public class CheckIfArrayIsSorted {
// THIS CODE IS FOR NON-DESCENDING ARRAY
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of array : ");
int size = sc.nextInt();
int[] arr = new int[size];
System.out.println("\nEnter the array elements : ");
for (int i = 0; i < arr.length; i++) {
System.out.printf("Element %d : ", i);
arr[i] = sc.nextInt();
}
System.out.print("\nArray : ");
for (int i=0;i<arr.length;i++) {
System.out.print(arr[i] + " ");
}
boolean sorted = true;
for (int i=1;i<arr.length; i++) {
if (arr[i-1] <= arr[i]) {
}
else {
sorted = false;
break;
}
}
if (sorted){
System.out.println("\nThe Array is sorted");
}
else{
System.out.println("\nThe Array is not sorted");
}
}
}