Check if Array is Sorted?
#include <bits/stdc++.h>
using namespace std ;
bool sorted(int arr[],int n){
//Base case
if(n==0 || n==1) return true ;
if(arr[0] > arr[1]) return false ;
//Recursive case
bool smallarraysorted = sorted(arr+1 , n-1);
// Calculation
if(smallarraysorted) return true ;
else return false ;
}
int main(){
int arr[] = {1,2,3,5,4} ;
int n = 5 ;
cout << sorted(arr,n) ;
return 0;
}
Comments
Post a Comment