JavaScript Program to Check an Array is Sorted or Not (original) (raw)
Last Updated : 02 Jul, 2024
In this article, we will learn how to check if the array is sorted in JavaScript. **JavaScript Array is a single variable that is used to store elements of different data types. Now, we have to find whether the array is sorted or not.
**Examples:
**Input : const arr = [1, 2, 3, 4, 5];
**Output : true
**Input : const arr = [3, 1, 4, 2, 5];
**Output : false
Below are the following approaches through which we can check if the array is sorted or not:
Table of Content
- Using the Brute Force Approach
- Using the every() Method
- Using sort() Method
- Using Recursion
- Using Array.reduce()
- Using Array.from() and every()
**Using the Brute Force Approach
In this approach, we iterate through the array and compare each element with the next one. If the first element is greater than the next element then we return false, else return true.
**Example:
JavaScript `
function checkSorted(arr) { for (let i = 0; i < arr.length - 1; i++) { if (arr[i] > arr[i + 1]) { return false; } } return true; }
// Example usage const arr1 = [32, 39, 48, 56]; const arr2 = [22, 65, 1, 39];
console.log(checkSorted(arr1));
console.log(checkSorted(arr2));
`
**Time Complexity: O(n)
**Auxiliary Space: O(1)
**Using the every()
Method
This is also a kind of iterative method but in this approach we use every() Method for iteration instead of for loop. JavaScript has many inbuilt methods. The Javascript **Array.every() method considers all the elements of an array and then further checks whether all the elements of the array satisfy the given condition (passed by in user) or not that is provided by a method passed to it as the argument.
**Example:
JavaScript `
function checkSorted(arr) { return arr.every((value, index, array) => index === 0 || value >= array[index - 1]); } // Example usage const arr1 = [32, 39, 48, 56]; const arr2 = [22, 65, 1, 39];
console.log(checkSorted(arr1)); console.log(checkSorted(arr2));
`
**Time Complexity: O(nlogn)
**Auxiliary Space: O(1)
**Using sort() Method
This is also very simple method but it is not optimised approach. In this method, we sort the array by using the JavaScript sort() inbuilt method and then compares with the original array that both array are equal or not. If they are equal, then array is sorted.
**Example:
JavaScript `
function checkSorted(arr) { const sortArr = [...arr].sort((a, b) => a - b); return JSON.stringify(arr) === JSON.stringify(sortArr);
}
const arr1 = [32, 39, 48, 56]; const arr2 = [22, 65, 1, 39];
console.log(checkSorted(arr1)); console.log(checkSorted(arr2));
`
**Time Complexity: O(nlogn)
**Auxiliary Space: O(n)
Using Recursion
- If we reach the end of the array then it is sorted, this is the base case for recursion.
- Now, check if the current element is greater than the next element then return false.
- Otherwise, call the recursive function for next pair of element.
**Example:
JavaScript `
function checkSorted(arr, i = 0) { //base case if (i === arr.length - 1) { return true; } if (arr[i] > arr[i + 1]) { return false; }
// Recursive case
return checkSorted(arr, i + 1);
}
const arr1 = [32, 39, 48, 56]; const arr2 = [22, 65, 1, 39];
console.log(checkSorted(arr1)); console.log(checkSorted(arr2));
`
Using Array.reduce()
Using Array.reduce(), the function checks if each element is greater than or equal to the previous one. Starting with true, it iterates through the array, updating the isSorted flag. If any element is out of order, it returns false; otherwise, it remains `true`.
**Example:
JavaScript `
function isSorted(arr) { return arr.reduce((isSorted, value, index) => { return isSorted && (index === 0 || arr[index - 1] <= value); }, true); }
console.log(isSorted([1, 2, 3, 4, 5])); console.log(isSorted([5, 3, 1, 2, 4]));
`
Using Array.from() and every()
Using Array.from() and every() to check if an array is sorted involves creating an array of indices, then using every() to ensure each element is less than or equal to the next. This method leverages the power of array iteration and functional programming.
**Example
JavaScript `
function isSorted(arr) { return Array.from({ length: arr.length - 1 }, (_, i) => i).every(i => arr[i] <= arr[i + 1]); }
console.log(isSorted([1, 2, 3, 4])); console.log(isSorted([1, 3, 2, 4]));
`