Search This Blog

2024/04/08

Javascript:Check if Array is sorted

Problem Statement:
Given an array of number find out if it is sorted and in which order.
E.g.If input is [ 22, 25, -6, 8, 67, 100 ] then output should be not sorted.
If input is [ -6, 8, 22, 25, 67, 100 ] then output should be sorted in ascending order.
If input is [ 100, 67, 25, 22, 2 ] then output should be sorted in descending order.

Solution:
function isArraySorted(arr, sortDirection = "asc") {
//create an array with [0,1,2,..,length-1]
let rangeArray = Array.from(Array(arr.length).keys());

//ascending order sorted
if (sortDirection == "asc") {
//in ascending order previous element is lesser than next element otherwise not sorted
let isSortedInAscendingOrder = rangeArray.reduce((accumulator, element) => {
if (element < arr.length && arr[element] > arr[element + 1]) {
accumulator = false;
}
return accumulator;
}, true);
return isSortedInAscendingOrder;
}

//descending order sorted
if (sortDirection == "desc") {
let isSortedInDescendingOrder = rangeArray.reduce(
(accumulator, element) => {
//in descending order previous element is greater than next element otherwise not sorted
if (element < arr.length && arr[element] < arr[element + 1]) {
accumulator = false;
}
return accumulator;
},
true
);
return isSortedInDescendingOrder;
}

return false;
}

//Driver code

//not sorted array as input
let notSorted = [22, 25, -6, 8, 67, 100];
console.log("Not Sorted Array as Input:", notSorted);

let notSortedisAscendingSorted = isArraySorted(notSorted, "asc");
console.log("Array is sorted in Ascending Order:", notSortedisAscendingSorted);

let notSortedIsDescendingSorted = isArraySorted(notSorted, "desc");
console.log("Array is sorted in Descending Order:", notSortedisAscendingSorted);

console.log('------Ascending Sorted Array As Input-------------')

//ascending sorted array as input
let ascendingSorted = [-6, 8, 22, 25, 67, 100];
console.log("Ascending Sorted Array as Input:", ascendingSorted);

let ascendingSortedisAscendingSorted = isArraySorted(ascendingSorted, "asc");
console.log(
"Array is sorted in Ascending Order:",
ascendingSortedisAscendingSorted
);

let ascendingSortedIsDescendingSorted = isArraySorted(ascendingSorted, "desc");
console.log(
"Array is sorted in Descending Order:",
ascendingSortedIsDescendingSorted
);

console.log('------Descending Sorted Array As Input-------------')
//descending sorted array as input
let descendingSorted = [100, 67, 25, 22, 8 - 6];
console.log("Descending Sorted Array as Input:", descendingSorted);

let descendingSortedisAscendingSorted = isArraySorted(descendingSorted, "asc");
console.log(
"Array is sorted in Ascending Order:",
descendingSortedisAscendingSorted
);

let descendingSortedIsDescendingSorted = isArraySorted(descendingSorted, "desc");
console.log(
"Array is sorted in Descending Order:",
descendingSortedIsDescendingSorted
);

Output:
Not Sorted Array as Input: [ 22, 25, -6, 8, 67, 100 ]
Array is sorted in Ascending Order: false
Array is sorted in Descending Order: false
------Ascending Sorted Array As Input-------------
Ascending Sorted Array as Input: [ -6, 8, 22, 25, 67, 100 ]
Array is sorted in Ascending Order: true
Array is sorted in Descending Order: false
------Descending Sorted Array As Input-------------
Descending Sorted Array as Input: [ 100, 67, 25, 22, 2 ]
Array is sorted in Ascending Order: false
Array is sorted in Descending Order: true


Note:
If array has only one element or all elements are same then it can be
considered as sorted in both ascending & descending order.

No comments:

Post a Comment