-
Notifications
You must be signed in to change notification settings - Fork 1
/
912.ts
37 lines (31 loc) · 782 Bytes
/
912.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
* @lc app=leetcode.cn id=912 lang=typescript
*
* [912] 排序数组
*/
// @lc code=start
function sortArray(nums: number[]): number[] {
function quickSort(left: number, right: number) {
if (left < right) {
const mid = partition(left, right)
quickSort(left, mid - 1)
quickSort(mid + 1, right)
}
}
function partition(left: number, right: number) {
let tmp = nums[left]
while (left < right) {
while (left < right && nums[right] > tmp) right--
nums[left] = nums[right]
while (left < right && nums[left] <= tmp) left++
nums[right] = nums[left]
}
nums[left] = tmp
return left
}
quickSort(0, nums.length - 1)
return nums
};
// @lc code=end
let nums = [5,1,1,2,0,0]
console.log(sortArray(nums))