Created
September 27, 2021 14:59
快排 js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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