Skip to content

Instantly share code, notes, and snippets.

@antoniopicone
Created October 21, 2017 14:20
Show Gist options
  • Save antoniopicone/e5911a602cb779911f0c22c822554eed to your computer and use it in GitHub Desktop.
Save antoniopicone/e5911a602cb779911f0c22c822554eed to your computer and use it in GitHub Desktop.
Array.prototype.swap = function(x,y) {
if(x == undefined || y == undefined) return;
var temp = this[x];
this[x] = this[y];
this[y] = temp;
};
var Partition = function(A,p,r) {
var x = A[r];
var i=p-1;
for (var j=p; j<r;j++) {
if(A[j]<A[r]) {
i++;
A.swap(i,j);
}
}
A.swap(i+1,r);
return i+1;
};
var QuickSort = function(A,p,r) {
if (p<r) {
var q = Partition(A,p,r);
QuickSort(A,p,q-1);
QuickSort(A,q+1,r);
}
};
var arr = [null,3,4,1,2,8,6,9,5,7];
console.log(arr);
QuickSort(arr,1,9);
console.log(arr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment