Skip to content
Advertisement

Check if array is sorted using recursion

I am getting true as answer even for unsorted(isNonDescending) arrays. Where is the bug? I want to break the array into smaller problem from the start of the array only.

//Check for isNonDescending.

JavaScript

Advertisement

Answer

Instead of passing the size of the array as a parameter, which makes no sense anyway, because you can simply call arr.length, you should pass a starting index and increase it with each recursive call until you have reached the length of your array.

JavaScript

and call from main with 0 as a starting index

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement