Skip to content

Instantly share code, notes, and snippets.

@hdemon
Created August 28, 2011 11:39
Show Gist options
  • Save hdemon/1176573 to your computer and use it in GitHub Desktop.
Save hdemon/1176573 to your computer and use it in GitHub Desktop.
quicksort.js
function quickSort(array, start, end){
var start = ( typeof start === "undefined" ? 0 : start),
end = ( typeof end === "undefined" ? array.length - 1 : end),
p, tmp,
L, R;
p = array[ Math.floor((end + start) / 2) ];
L = start;
R = end;
while (true){
while ( array[L] < p ) { L++; }
while ( array[R] > p ) { R--; }
if ( L >= R ) break;
tmp = array[L];
array[L] = array[R];
array[R] = tmp;
L++;
R--;
}
if (start < L-1) quickSort(array, start, L-1);
if (R+1 < end) quickSort(array, R+1, end);
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment