How to check if an Array is sorted?

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.

Example:

Input : 20 20 45 89 89 90 
Output: Yes 

Input : 20 20 78 98 99 97 
Output : No
  • First, check if elements are already sorted or length of array is greater than 1.

  • If it is unsorted, implement a sorting algorithm like Bubble Sort or Selection sort and compare every element with previous element for each iteration and switch the elements until the array gets sorted.