-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChecksortedarray.java
More file actions
48 lines (46 loc) · 1.16 KB
/
Copy pathChecksortedarray.java
File metadata and controls
48 lines (46 loc) · 1.16 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
47
48
import java.util.Scanner;
public class Checksortedarray {
//from recursion
public static boolean sort(int arr[],int index){
if(index==(arr.length-1))
{
return true;
}
if(arr[index]>=arr[index+1])
{
return false;
}
else{
return sort(arr,index+1);
}
}
public static void main(String[] args) {
//from loop
Scanner s=new Scanner(System.in);
int n,flag=0;
System.out.println("enter size of array");
n=s.nextInt();
int arr[]=new int[n];
System.out.println("enter elements of array");
for(int i=0;i<n;i++)
{
arr[i]=s.nextInt();
}
System.out.println(sort(arr,0));
for(int i=0;i<n-1;i++)
{
if(arr[i]>arr[i+1] || arr[i]==arr[i+1])
{
flag=1;
break;
}
}
if(flag==1)
{
System.out.println("not sorted array");
}
else{
System.out.println("sorted array");
}
}
}