Is Sorted
#include <iostream>using namespace std;
bool isSorted(int arr[], int size) {
// Base case: If the array is empty (size == 0) or has only one element (size == 1), // it is considered sorted, so return true. if(size == 0 || size == 1) { return true; }
// Check if the first element is greater than the second element. // If so, the array is not sorted, so return false. if(arr[0] > arr[1]) return false;
// Recursive case: // If the first two elements are in order, recursively check the remaining part of the array // (i.e., the array starting from the next element, `arr + 1`, with size reduced by 1). bool remainingPart = isSorted(arr + 1, size - 1);
// Return the result of the recursive check for the remaining part of the array. return remainingPart;}
int main() { int arr[] = {1, 2, 3, 4, 5}; // Example: sorted array int size = sizeof(arr) / sizeof(arr[0]);
if(isSorted(arr, size)) { cout << "Array is sorted." << endl; } else { cout << "Array is not sorted." << endl; }
return 0;}Example Walkthrough:
Consider the input array arr[] = {1, 2, 3, 4, 5} and size = 5:
- First Call:
isSorted(arr, 5)arr[0] = 1,arr[1] = 2, since1 <= 2, callisSorted(arr + 1, 4).
- Second Call:
isSorted(arr + 1, 4)- Now
arr[0] = 2,arr[1] = 3, since2 <= 3, callisSorted(arr + 1, 3).
- Now
- Third Call:
isSorted(arr + 2, 3)- Now
arr[0] = 3,arr[1] = 4, since3 <= 4, callisSorted(arr + 1, 2).
- Now
- Fourth Call:
isSorted(arr + 3, 2)- Now
arr[0] = 4,arr[1] = 5, since4 <= 5, callisSorted(arr + 1, 1).
- Now
- Fifth Call:
isSorted(arr + 4, 1)- Base case: Since
size = 1, returntrue.
- Base case: Since
Output
Array is sorted.Example with Unsorted Array:
For arr[] = {1, 3, 2, 4} and size = 4:
- First Call:
isSorted(arr, 4)arr[0] = 1,arr[1] = 3, since1 <= 3, callisSorted(arr + 1, 3).
- Second Call:
isSorted(arr + 1, 3)- Now
arr[0] = 3,arr[1] = 2, since3 > 2, returnfalse
- Now
Output
Array is not sorted.