VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-check-array-sorted-not-iterative-recursive/

⇱ Check if an Array is Sorted - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if an Array is Sorted

Last Updated : 28 Mar, 2026

Given an array arr[], check if it is sorted in ascending order or not. Equal values are allowed in an array and two consecutive equal values are considered sorted.

Examples:

Input: arr[] = [10, 20, 30, 40, 50]
Output: true
Explanation: The given array is sorted.

Input: arr[] = [90, 80, 100, 70, 40, 30]
Output: false
Explanation: The given array is not sorted.

Iterative approach - O(n) Time and O(1) Space

We traverse from the second element. For every element we check if it is smaller than or equal to previous element or not. At any point if we find previous element greater, we return false.

For example: arr[] = [10, 20, 30, 5, 6]

i = 1 : (10 <= 20), continue
i = 2 : (20 <= 30), continue
i = 3 : (30 > 5), return false.


Output
true

Recursive approach - O(n) Time and O(n) Space

The idea is to check if the last two elements are in order, then recursively check the rest of the array. The base case is when the array has zero or one element, which is always considered sorted.

Step-By-Step Approach:

  • If size of array is zero or one, return true.
  • Check last two elements of array, if they are sorted, perform a recursive call with n-1 else, return false.

Output
true

Using Built-in Methods (Applicable for C++ and Python Only) - O(n) Time and O(1) Space

The idea behind here is we use built-in methods in STL of C++ and Python to check if an array is sorted or not.


Output
true
Comment
Article Tags: