Skip to content

Instantly share code, notes, and snippets.

@vaidehijoshi
Last active May 7, 2018 19:12
Show Gist options
  • Save vaidehijoshi/4be2e215014b2efcb96848735a91d0b8 to your computer and use it in GitHub Desktop.
Save vaidehijoshi/4be2e215014b2efcb96848735a91d0b8 to your computer and use it in GitHub Desktop.
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;
}
@ivschukin
Copy link

ivschukin commented May 7, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment