Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick-Sort in javascript
function quickSort(a, low, high){
if(high > low){
var index = getRandomInt(low,high);
//console.log(low,high,index);
var pivot = a[index];
//console.log("pivot",pivot);
a = partition(a,pivot);
//console.log(a);
quickSort(a,low,index-1);
quickSort(a,index+1,high);
}
return a;
}
function partition (a,pivot) {
var i = 0;
for( var j=0; j < a.length; j++ ){
if( a[j]!= pivot && a[j] < pivot ){
var temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
}
}
return a;
}
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var arr = [1,3,455,45,32,23,345,43,2,23,234,34];
quickSort(arr,0,arr.length);
@Pinhead877

This comment has been minimized.

Show comment
Hide comment
@Pinhead877

Pinhead877 Jul 15, 2016

I copy pasted it with the test array and it didnt sort it right
my ouput:

[1, 3, 2, 23, 32, 23, 34, 45, 234, 43, 345, 455]

I copy pasted it with the test array and it didnt sort it right
my ouput:

[1, 3, 2, 23, 32, 23, 34, 45, 234, 43, 345, 455]

@mayrarodriguez1709

This comment has been minimized.

Show comment
Hide comment
@mayrarodriguez1709

mayrarodriguez1709 Aug 30, 2016

Yes, that's algoritm is wrong

Yes, that's algoritm is wrong

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