Skip to content

Instantly share code, notes, and snippets.

@lijiext
Created September 27, 2021 14:59
Show Gist options
  • Save lijiext/8d1408efa0cbb410537d7a3f06a76bc0 to your computer and use it in GitHub Desktop.
Save lijiext/8d1408efa0cbb410537d7a3f06a76bc0 to your computer and use it in GitHub Desktop.
快排 js
let nums = [5, 456, 12, 5, 12, 8, 2, 5, 12, 645, 125, 645, 1, 512, 641, 3132];
let MAX = 10000;
for (let i = 1; i <= MAX; i++) {
nums.push(Math.floor(Math.random() * 10000 + 1));
}
function quick_sort(nums, left, right) {
// console.log(left,right)
if (left < right) {
let flag = nums[left];
let i = left,
j = right;
while (i !== j) {
while (j > i && nums[j] > flag) {
j--;
}
if (i < j) {
nums[i] = nums[j];
i++;
}
while (j > i && nums[i] < flag) {
i++;
}
if (i < j) {
nums[j] = nums[i];
j--;
}
}
nums[i] = flag;
// console.log(nums)
quick_sort(nums, left, i - 1);
quick_sort(nums, i + 1, right);
}
}
quick_sort(nums, 0, nums.length - 1);
console.log(nums);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment