# Check if Array is Sorted or Not

“** Check if Array is Sorted or Not**” is a basic problem of array data structure. Here, we are given an array of size ‘n’ and our task is to check if array is sorted or not.

**Example:**

Arr[9] = {11, 22, 33, 44, 55, 66, 77, 88, 99}INPUT:Array is sorted.OUTPUT:Arr[9] = {11, 33, 55, 77, 99, 22, 44, 66, 88}INPUT:Array is not sorted.OUTPUT:

The steps required to check if array is sorted or not are as follows:

- Scan array size and array elements.
- Iterate over all the elements of array and check if every subsequent element is greater than or equal to current element or not. If yes, then array is sorted else array is not sorted.

**C++ Program to check if array is sorted or not is as follows:**

/* Program to check if array is sorted or not */ #include<bits/stdc++.h> using namespace std; int main() { /* Scan size of array */ int n; cout<<"Enter the size of an array: "; cin>>n; /* Scan Array Elements */ int arr[n]; cout<<"\nEnter array elements:\n"; for(int i = 0; i < n; i++) { cin>>arr[i]; } int temp = 0; for(int i = 1; i < n; i++) { /* If array is not sorted */ if(arr[i] < arr[i-1]) { temp = 1; break; } } if(temp == 1) cout<<"\nArray is not sorted!!"; else cout<<"\nArray is sorted!!"; }

Enter the size of an array: 5 Enter array elements: 11 22 33 44 55 Array is sorted!!OUTPUT:O(n), where n is the length of arrayTime Complexity:

**Related Posts:**

**Program to print Alternate Elements of an Array.****Program to swap kth element from beginning to kth element from end in an Array.****Program to print all possible subarrays of the given array.****Program to print kth smallest and kth largest element of an Array.****Program to find equilibrium index of an Array.****Program to find majority element of an Array.****Program to find mean of the Array.****Program to sort an Array of 0s and 1s.****Program to Reverse an Array.****Program to count number of odd numbers and even numbers in an array.****Program to find absolute difference between sum of odd index elements and even index elements.****Program to find smallest and largest element of the array.****Program to count occurrences of a particular element in an array.****Program to search an element in an Array (Linear Search).****Program to Rotate Array Elements by ‘D’ positions.****Program to find most frequent element in an array.****Program to find pair in an array with given sum.****Program to find first repeating element of an array.****Program to merge two sorted arrays.****Program to find missing number in an array.**