Created
October 4, 2014 07:26
-
-
Save jjmerino/3591539bb9e8f1a15047 to your computer and use it in GitHub Desktop.
Quick sort javascript
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
// A swap function | |
var swap = function(array,i,j){ | |
var aux = array[i]; | |
array[i] = array[j]; | |
array[j] = aux; | |
}; | |
// Quick sort implementation | |
var quickSort = function(array) { | |
var f = function(from,to){ | |
if(to-from<=2){ | |
return; | |
} | |
var pivot = from; | |
var socket = from+1; | |
for(var i = from+1; i < to; i++){ | |
if(+array[pivot]>+array[i]){ | |
swap(array,socket,i); | |
socket++; | |
} | |
} | |
swap(array,pivot,socket-1); | |
pivot = socket; | |
// recursivelly sort left and right of the pivot | |
f(from,pivot); | |
f(pivot,to); | |
}; | |
f(0,array.length); | |
return array; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment