int isArrayInSortedOrder(int A[], int n, int index){
if(n == 1){
return 1;
}
if(index == n -2){ //for last 2 elements example: 6 elements, index = 4;=> 4==(6-2)=>true; A[4]<A[5];
return A[index] < A[index + 1];
}
if(A[index] > A[index + 1]){
return 0;
}
return isArrayInSortedOrder(A, n, index + 1);
}
Time Complexity: O(n)
Space Complexity: O(n) for stack space
Otherway:
int isArrayInSortedOrder(int A[],int N){
if(N == 1)return 1;
return (A[N-1]<A[N-2])?0:isArrayInSortedOrder(A,N-1);
}
if(n == 1){
return 1;
}
if(index == n -2){ //for last 2 elements example: 6 elements, index = 4;=> 4==(6-2)=>true; A[4]<A[5];
return A[index] < A[index + 1];
}
if(A[index] > A[index + 1]){
return 0;
}
return isArrayInSortedOrder(A, n, index + 1);
}
Time Complexity: O(n)
Space Complexity: O(n) for stack space
Otherway:
int isArrayInSortedOrder(int A[],int N){
if(N == 1)return 1;
return (A[N-1]<A[N-2])?0:isArrayInSortedOrder(A,N-1);
}
No comments:
Post a Comment