Open In App

Check if an Array is Sorted

Last Updated : 07 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array of size n, the task is to 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[] = [20, 21, 45, 89, 89, 90]
Output: Yes

Input: arr[] = [20, 20, 45, 89, 89, 90]
Output: Yes

Input: arr[] = [20, 20, 78, 98, 99, 97]
Output: No

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

The idea is simple. We start traversing 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 [10, 20, 30, 5, 6[

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

C++
#include<bits/stdc++.h>usingnamespacestd;// Function that returns true if vector is// sorted in non-decreasing order.boolisSorted(constvector<int>&arr){// For an array to be sorted, every// element must be greater than the // previous elementfor(inti=1;i<arr.size();i++)if(arr[i-1]>arr[i])returnfalse;returntrue;}// Driver codeintmain(){vector<int>arr={20,23,23,45,78,88};cout<<(isSorted(arr)?"Yes\n":"No\n");return0;}cout<<(isSorted(arr)?"Yes\n":"No\n");return0;}
C
// C program to check if an// Array is sorted or not#include<stdio.h>// Function that returns true if array is// sorted in non-decreasing order.intarraySortedOrNot(intarr[],intn){// Array has one or no elementif(n==0||n==1)return1;for(inti=1;i<n;i++){// Unsorted pair foundif(arr[i-1]>arr[i])return0;}// No unsorted pair foundreturn1;}// Driver codeintmain(){intarr[]={20,23,23,45,78,88};intn=sizeof(arr)/sizeof(arr[0]);if(arraySortedOrNot(arr,n))printf("Yes\n");elseprintf("No\n");return0;}
Java
// Recursive approach to check if an// Array is sorted or notclassGFG{// Function that returns true if array is// sorted in non-decreasing order.staticbooleanarraySortedOrNot(intarr[],intn){// Array has one or no elementif(n==0||n==1)returntrue;for(inti=1;i<n;i++)// Unsorted pair foundif(arr[i-1]>arr[i])returnfalse;// No unsorted pair foundreturntrue;}// driver codepublicstaticvoidmain(String[]args){intarr[]={20,23,23,45,78,88};intn=arr.length;if(arraySortedOrNot(arr,n))System.out.print("Yes\n");elseSystem.out.print("No\n");}}// This code is contributed by Anant Agarwal.
Python
# Python3 program to check if an# Array is sorted or not# Function that returns true if array is# sorted in non-decreasing order.defarraySortedOrNot(arr,n):# Array has one or no elementif(n==0orn==1):returnTrueforiinrange(1,n):# Unsorted pair foundif(arr[i-1]>arr[i]):returnFalse# No unsorted pair foundreturnTrue# Driver codearr=[20,23,23,45,78,88]n=len(arr)if(arraySortedOrNot(arr,n)):print("Yes")else:print("No")# This code is contributed by Anant Agarwal.
C#
// Recursive approach to check if an// Array is sorted or notusingSystem;classGFG{// Function that returns true if array is// sorted in non-decreasing order.staticboolarraySortedOrNot(int[]arr,intn){// Array has one or no elementif(n==0||n==1)returntrue;for(inti=1;i<n;i++)// Unsorted pair foundif(arr[i-1]>arr[i])returnfalse;// No unsorted pair foundreturntrue;}// Driver codepublicstaticvoidMain(String[]args){int[]arr={20,23,23,45,78,88};intn=arr.Length;if(arraySortedOrNot(arr,n))Console.Write("Yes\n");elseConsole.Write("No\n");}}// This code is contributed by PrinciRaj1992
JavaScript
// JavaScript program Iterative approach to check if an// Array is sorted or not// Function that returns true if array is// sorted in non-decreasing order.functionarraySortedOrNot(arr,n)// Array has one or no elementif(n===0||n===1){returntrue;}for(leti=1;i<n;i++){// Unsorted pair foundif(arr[i-1]>arr[i]){returnfalse;}}// No unsorted pair foundreturntrue;}// Driver Codeletarr=[20,23,23,45,78,88];letn=arr.length;if(arraySortedOrNot(arr,n)){console.log("Yes");}else{console.log("No");}

Output
Yes

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

The basic idea for the recursive 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.
C++
#include<iostream>#include<vector>usingnamespacestd;// Recursive function that returns true if vector is// sorted in non-decreasing order.boolisSorted(constvector<int>&arr,intn){// Base caseif(n==1||n==0)returntrue;// Check if current and previous elements are in order// and recursively check the rest of the arrayreturnarr[n-1]>=arr[n-2]&&isSorted(arr,n-1);}// Driver codeintmain(){vector<int>arr={20,23,23,45,78,88};cout<<(isSorted(arr,arr.size())?"Yes\n":"No\n");return0;}
C
// C Recursive approach to check if an// Array is sorted or not#include<stdio.h>// Function that returns true if array is// sorted in non-decreasing order.intarraySortedOrNot(inta[],intn){// Base caseif(n==1||n==0){return1;}// Check if present index and index// previous to it are in correct order// and rest of the array is also sorted// if true then return true else return// falsereturna[n-1]>=a[n-2]&&arraySortedOrNot(a,n-1);}// Driver codeintmain(){intarr[]={20,23,23,45,78,88};intn=sizeof(arr)/sizeof(arr[0]);// Function Callif(arraySortedOrNot(arr,n)){printf("Yes\n");}else{printf("No\n");}return0;}
Java
// Java Recursive approach to check if an// Array is sorted or notclassGFG{// Function that returns true if array is// sorted in non-decreasing order.staticbooleanarraySortedOrNot(inta[],intn){// base caseif(n==1||n==0)returntrue;// check if present index and index // previous to it are in correct order// and rest of the array is also sorted// if true then return true else return// falsereturna[n-1]>=a[n-2]&&arraySortedOrNot(a,n-1);}// Driver codepublicstaticvoidmain(String[]args){intarr[]={20,23,23,45,78,88};intn=arr.length;// Function Callif(arraySortedOrNot(arr,n))System.out.print("Yes");elseSystem.out.print("No");}}// This code is contributed by Durgesh N. Birmiwal.
Python
# Python3 recursive program to check# if an Array is sorted or not# Function that returns true if array # is sorted in non-decreasing order.defarraySortedOrNot(arr,n):# Base caseif(n==0orn==1):returnTrue# Check if present index and index # previous to it are in correct order# and rest of the array is also sorted# if true then return true else return# falsereturn(arr[n-1]>=arr[n-2]andarraySortedOrNot(arr,n-1))# Driver codearr=[20,23,23,45,78,88]n=len(arr)# Function Callif(arraySortedOrNot(arr,n)):print("Yes")else:print("No")# This code is contributed by Virusbuddah
C#
// C# recursive approach to check if an// Array is sorted or notusingSystem;classGFG{// Function that returns true if array is// sorted in non-decreasing order.staticboolarraySortedOrNot(int[]a,intn){// Base caseif(n==1||n==0){returntrue;}// Check if present index and index // previous to it are in correct order// and rest of the array is also sorted// if true then return true else return// falsereturna[n-1]>=a[n-2]&&arraySortedOrNot(a,n-1);}// Driver codestaticpublicvoidMain(){int[]arr={20,23,23,45,78,88};intn=arr.Length;// Function Callif(arraySortedOrNot(arr,n)){Console.WriteLine("Yes");}else{Console.WriteLine("No");}}}// This code is contributed by rag2127
JavaScript
// JavaScript Recursive approach to check if an// Array is sorted or not// Function that returns true if array is// sorted in non-decreasing order.functionarraySortedOrNot(a,n){// Base caseif(n==1||n==0){returntrue;}// Check if present index and index// previous to it are in correct order// and rest of the array is also sorted// if true then return true else return// falsereturna[n-1]>=a[n-2]&&arraySortedOrNot(a,n-1);}// Driver codeletarr=[20,23,23,45,78,88];letn=arr.length;// Function Callif(arraySortedOrNot(arr,n)){console.log("Yes");}else{console.log("No");}

Output
Yes

Time Complexity:O(n) 
Auxiliary Space: O(n) for Recursion Call Stack.



Next Article
Article Tags :
Practice Tags :

Similar Reads

close