Check if Array is Sorted?

 

#include <bits/stdc++.h>
using namespace std ;

bool sorted(int arr[],int n){
    //Base case
    if(n==0 || n==1return true ;
    if(arr[0] > arr[1]) return false ;
    
    //Recursive case
    bool smallarraysorted = sorted(arr+1 , n-1);

    // Calculation
    if(smallarraysortedreturn true ;
    else return false ;

}

int main(){
    int arr[] = {1,2,3,5,4} ;
    int n = 5 ;
    cout << sorted(arr,n) ;
    return 0;
}


Comments

Popular posts from this blog

CodeChef::CSUB

How Recursion Works?

CodeChef::TREE2