Last active
May 7, 2018 19:12
-
-
Save vaidehijoshi/4be2e215014b2efcb96848735a91d0b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function quickSort(items, leftIndex, rightIndex) { | |
// Declare an index that will be our pivot reference. | |
var pivotIndex; | |
// If the array has only one item, it's already sorted! | |
// If it has no items, we don't want to try to sort it! | |
if (items.length > 1) { | |
// As long as the array has two items, we can parition it. | |
pivotIndex = partition(items, leftIndex, rightIndex); | |
console.log('** pivot is: ', items[pivotIndex]); | |
// If the left reference hasn't been incremented to | |
// reach the pivot yet, we want to keep comparing it. | |
if (leftIndex < pivotIndex - 1) { | |
quickSort(items, leftIndex, pivotIndex - 1); | |
} | |
// If the right reference hasn't reached the | |
// pivotIndex yet, we need to keep comparing it. | |
if (pivotIndex < rightIndex) { | |
quickSort(items, pivotIndex, rightIndex); | |
} | |
} | |
return items; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually, the length of items will always be greater than one... Should produce a stack overflow... I guess you wanted to write leftIndex < rightIndex in line 7