Check if an array of numbers is sorted in Javascript

preview_player
Показать описание
Given an array of integers, check to see if the array is already sorted (return true or false).

Once you've solved it iteratively, try to solve it recursively or using functional programming. If you have time, write and walk through some test cases for your code. What's the time complexity of your solution?
Рекомендации по теме
Комментарии
Автор

For my recursive solution in the video, I know I had some redundant code and I didn't really need to set an accumulator. So here's my revised solution:

// recursive solution: O(n)
function isSorted(arr, index = 1){
if(index === arr.length){
return true;
}
return (arr[index] - arr[index-1] > 0) && isSorted(arr, index+1);
}

chengjieyun