Created
August 28, 2011 11:39
-
-
Save hdemon/1176573 to your computer and use it in GitHub Desktop.
quicksort.js
This file contains 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(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