-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathRecursion_CheckSorted.java
57 lines (43 loc) · 970 Bytes
/
Recursion_CheckSorted.java
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
49
50
51
52
53
54
55
56
57
public class Recursion_CheckSorted {
public static boolean checkSorted(int input[]){
if(input.length <= 1){
return true;
}
int smallInput[] = new int[input.length - 1];
for(int i = 1; i < input.length; i++){
smallInput[i - 1] = input[i];
}
boolean smallAns = checkSorted(smallInput);
if(!smallAns){
return false;
}
if(input[0] <= input[1]){
return true;
}else{
return false;
}
}
public static boolean checkSorted_2(int input[]){
if(input.length <= 1){
return true;
}
if(input[0] > input[1]){
return false;
}
int smallInput[] = new int[input.length - 1];
for(int i = 1; i < input.length; i++){
smallInput[i - 1] = input[i];
}
boolean smallAns = checkSorted_2(smallInput);
return smallAns;
// if(smallAns){
// return true;
// }else{
// return false;
// }
}
public static void main(String[] args) {
int input[] = {1,2,3};
System.out.println(checkSorted_2(input));
}
}