Skip to content

Instantly share code, notes, and snippets.

@karenpeng
Created January 31, 2015 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karenpeng/75e3225c68453b07498e to your computer and use it in GitHub Desktop.
Save karenpeng/75e3225c68453b07498e to your computer and use it in GitHub Desktop.
function quickSort(arr) {
var less = more = pivots = [];
if (arr.length > 1) {
pivot = arr[0];
arr.forEach(function (i) {
if (i < pivot) {
less.push(i);
console.log(i)
} else if (i > pivot) {
more.push(i);
} else {
pivots.push(i);
}
});
console.log('! ' + less)
//console.log(less + ' ! ' + pivots + ' ! ' + more)
} else {
return arr;
}
//quickSort(less);
//quickSort(more);
return less.concat(pivots).concat(more);
}
//this program will logs out
// 4
// 6
// 6
// 2
// 7
// ! 8,4,6,13,6,2,7,46,32,12,53,124
function quickSort(arr) {
var less = [],
more = [],
pivots = [];
if (arr.length > 1) {
pivot = arr[0];
arr.forEach(function (i) {
if (i < pivot) {
less.push(i);
console.log(i)
} else if (i > pivot) {
more.push(i);
} else {
pivots.push(i);
}
});
console.log('! ' + less)
} else {
return arr;
}
//quickSort(less);
//quickSort(more);
return less.concat(pivots).concat(more);
}
//while this program will logs out
// 4
// 6
// 6
// 2
// 7
// ! 4,6,6,2,7
/*
Be careful of the equal sign!!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment