Skip to content

Instantly share code, notes, and snippets.

@dondevi
Created February 8, 2018 09:32
Show Gist options
  • Save dondevi/9862e66cefc7766f618437e3cd65b669 to your computer and use it in GitHub Desktop.
Save dondevi/9862e66cefc7766f618437e3cd65b669 to your computer and use it in GitHub Desktop.
/**
* 快速排序
* @param {Array} array - 乱序数组
*/
function quickSort (array) {
if (0 === array.length) { return array; }
let key = array.shift();
let lessers = [];
let greaters = [];
for (let i = 0, item = null; item = array[i]; i++) {
item < key ? lessers.push(item) : greaters.push(item);
}
return [...quickSort(lessers), key, ...quickSort(greaters)];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment