Given an array of size n, write a program 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 : 20 21 45 89 89 90
Output : Yes
Input : 20 20 45 89 89 90
Output : Yes
Input : 20 20 78 98 99 97
Output : No
Approach 1: Recursive approach
The basic idea for the recursive approach:
1: If size of array is zero or one, return true.
2: Check last two elements of array, if they are
sorted, perform a recursive call with n-1
else, return false.
If all the elements will be found sorted, n will
eventually fall to one, satisfying Step 1.
Below is the implementation using recursion:
C
#include <stdio.h>
int arraySortedOrNot( int arr[], int n)
{
if (n == 1 || n == 0)
return 1;
if (arr[n - 1] < arr[n - 2])
return 0;
return arraySortedOrNot(arr, n - 1);
}
int main()
{
int arr[] = { 20, 23, 23, 45, 78, 88 };
int n = sizeof (arr) / sizeof (arr[0]);
if (arraySortedOrNot(arr, n))
printf ( "Yes\n" );
else
printf ( "No\n" );
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
int arraySortedOrNot( int arr[], int n)
{
if (n == 1 || n == 0)
return 1;
if (arr[n - 1] < arr[n - 2])
return 0;
return arraySortedOrNot(arr, n - 1);
}
int main()
{
int arr[] = { 20, 23, 23, 45, 78, 88 };
int n = sizeof (arr) / sizeof (arr[0]);
if (arraySortedOrNot(arr, n))
cout << "Yes\n" ;
else
cout << "No\n" ;
}
|
Java
class CkeckSorted {
static int arraySortedOrNot( int arr[], int n)
{
if (n == 1 || n == 0 )
return 1 ;
if (arr[n - 1 ] < arr[n - 2 ])
return 0 ;
return arraySortedOrNot(arr, n - 1 );
}
public static void main(String[] args)
{
int arr[] = { 20 , 23 , 23 , 45 , 78 , 88 };
int n = arr.length;
if (arraySortedOrNot(arr, n) != 0 )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def arraySortedOrNot(arr):
n = len (arr)
if n = = 1 or n = = 0 :
return True
return arr[ 0 ] < = arr[ 1 ] and arraySortedOrNot(arr[ 1 :])
arr = [ 20 , 23 , 23 , 45 , 78 , 88 ]
if arraySortedOrNot(arr):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class CkeckSorted {
static int arraySortedOrNot( int [] arr, int n)
{
if (n == 1 || n == 0)
return 1;
if (arr[n - 1] < arr[n - 2])
return 0;
return arraySortedOrNot(arr, n - 1);
}
public static void Main(String[] args)
{
int [] arr = { 20, 23, 23, 45, 78, 88 };
int n = arr.Length;
if (arraySortedOrNot(arr, n) != 0)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function arraySortedOrNot(arr, n)
{
if (n == 1 || n == 0)
return 1;
if (arr[n - 1] < arr[n - 2])
return 0;
return arraySortedOrNot(arr, n - 1);
}
let arr = [ 20, 23, 23, 45, 78, 88 ];
let n = arr.length;
if (arraySortedOrNot(arr, n) != 0)
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n) for Recursion Call Stack.
Another Recursive approach:
C
#include <stdio.h>
int arraySortedOrNot( int a[], int n)
{
if (n == 1 || n == 0) {
return 1;
}
return a[n - 1] >= a[n - 2]
&& arraySortedOrNot(a, n - 1);
}
int main()
{
int arr[] = { 20, 23, 23, 45, 78, 88 };
int n = sizeof (arr) / sizeof (arr[0]);
if (arraySortedOrNot(arr, n)) {
printf ( "Yes\n" );
}
else {
printf ( "No\n" );
}
return 0;
}
|
C++
#include <iostream>
using namespace std;
bool arraySortedOrNot( int a[], int n)
{
if (n == 1 || n == 0)
{
return true ;
}
return a[n - 1] >= a[n - 2] &&
arraySortedOrNot(a, n - 1);
}
int main()
{
int arr[] = { 20, 23, 23, 45, 78, 88 };
int n = sizeof (arr) / sizeof (arr[0]);
if (arraySortedOrNot(arr, n))
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
return 0;
}
|
Java
class GFG {
static boolean arraySortedOrNot( int a[], int n)
{
if (n == 1 || n == 0 )
return true ;
return a[n - 1 ] >= a[n - 2 ]
&& arraySortedOrNot(a, n - 1 );
}
public static void main(String[] args)
{
int arr[] = { 20 , 23 , 23 , 45 , 78 , 88 };
int n = arr.length;
if (arraySortedOrNot(arr, n))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def arraySortedOrNot(arr, n):
if (n = = 0 or n = = 1 ):
return True
return (arr[n - 1 ] > = arr[n - 2 ] and
arraySortedOrNot(arr, n - 1 ))
arr = [ 20 , 23 , 23 , 45 , 78 , 88 ]
n = len (arr)
if (arraySortedOrNot(arr, n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool arraySortedOrNot( int [] a, int n)
{
if (n == 1 || n == 0)
{
return true ;
}
return a[n - 1] >= a[n - 2] &&
arraySortedOrNot(a, n - 1);
}
static public void Main()
{
int [] arr = { 20, 23, 23, 45, 78, 88 };
int n = arr.Length;
if (arraySortedOrNot(arr, n))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
function arraySortedOrNot(a, n)
{
if (n == 1 || n == 0)
{
return true ;
}
return a[n - 1] >= a[n - 2] &&
arraySortedOrNot(a, n - 1);
}
let arr = [ 20, 23, 23, 45, 78, 88 ];
let n = arr.length;
if (arraySortedOrNot(arr, n))
{
document.write( "Yes" + "<br>" );
}
else
{
document.write( "No" + "<br>" );
}
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n) for Recursion Call Stack.
Approach 2: Iterative approach
The idea is pretty much the same. The benefit of the iterative approach is it avoids the usage of recursion stack space and recursion overhead.
Below is the implementation using iteration:
C
#include <stdio.h>
int arraySortedOrNot( int arr[], int n)
{
if (n == 0 || n == 1)
return 1;
for ( int i = 1; i < n; i++) {
if (arr[i - 1] > arr[i])
return 0;
}
return 1;
}
int main()
{
int arr[] = { 20, 23, 23, 45, 78, 88 };
int n = sizeof (arr) / sizeof (arr[0]);
if (arraySortedOrNot(arr, n))
printf ( "Yes\n" );
else
printf ( "No\n" );
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
bool arraySortedOrNot( int arr[], int n)
{
if (n == 0 || n == 1)
return true ;
for ( int i = 1; i < n; i++)
if (arr[i - 1] > arr[i])
return false ;
return true ;
}
int main()
{
int arr[] = { 20, 23, 23, 45, 78, 88 };
int n = sizeof (arr) / sizeof (arr[0]);
if (arraySortedOrNot(arr, n))
cout << "Yes\n" ;
else
cout << "No\n" ;
}
|
Java
class GFG {
static boolean arraySortedOrNot( int arr[], int n)
{
if (n == 0 || n == 1 )
return true ;
for ( int i = 1 ; i < n; i++)
if (arr[i - 1 ] > arr[i])
return false ;
return true ;
}
public static void main(String[] args)
{
int arr[] = { 20 , 23 , 23 , 45 , 78 , 88 };
int n = arr.length;
if (arraySortedOrNot(arr, n))
System.out.print( "Yes\n" );
else
System.out.print( "No\n" );
}
}
|
Python3
def arraySortedOrNot(arr, n):
if (n = = 0 or n = = 1 ):
return True
for i in range ( 1 , n):
if (arr[i - 1 ] > arr[i]):
return False
return True
arr = [ 20 , 23 , 23 , 45 , 78 , 88 ]
n = len (arr)
if (arraySortedOrNot(arr, n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool arraySortedOrNot( int []arr, int n)
{
if (n == 0 || n == 1)
return true ;
for ( int i = 1; i < n; i++)
if (arr[i - 1] > arr[i])
return false ;
return true ;
}
public static void Main(String[] args)
{
int []arr = { 20, 23, 23, 45, 78, 88 };
int n = arr.Length;
if (arraySortedOrNot(arr, n))
Console.Write( "Yes\n" );
else
Console.Write( "No\n" );
}
}
|
Javascript
<script>
function arraySortedOrNot(arr, n)
{
if (n == 0 || n == 1)
return true ;
for (let i = 1; i < n; i++)
if (arr[i - 1] > arr[i])
return false ;
return true ;
}
let arr = [ 20, 23, 23, 45, 78, 88 ];
let n = arr.length;
if (arraySortedOrNot(arr, n))
document.write( "Yes\n" );
else
document.write( "No\n" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
15 Jun, 2023
Like Article
Save Article