Skip to content

Commit e714cc2

Browse files
committed
add quicksort and kthnum
1 parent 3550204 commit e714cc2

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

typescript/12_sorts/KthNum.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* O(n)的时间复杂度内求无序数组的第K大元素
3+
* 如[4,2,5,12,3]的第3大元素就是4
4+
* 这里也是使用了分治和分区的思想
5+
*/
6+
class KthNum {
7+
getKthNum(array: number[], k: number): number {
8+
const length = array.length
9+
if (k > length) return -1
10+
// q+1对应的元素一定比q和之前的元素大,q+1就是第q+1大元素
11+
// 注意返回的q是数组下标,所以我们要加1才能表示第k个元素
12+
let q = this.partition(array, 0, array.length - 1)
13+
while (q + 1 !== k) {
14+
if (q + 1 > k) {
15+
q = this.partition(array, 0, q - 1)
16+
} else {
17+
q = this.partition(array, q + 1, length - 1)
18+
}
19+
}
20+
return array[q]
21+
}
22+
23+
/**
24+
* 这里和快速排序一样
25+
* @param array 数组的一部分
26+
* @param p 开始坐标
27+
* @param r 结束坐标
28+
*/
29+
private partition(array: number[], p: number, r: number) {
30+
const pivot = array[p]
31+
let index = p + 1
32+
for (let i = index; i <= r; i++) {
33+
if (array[i] < pivot) {
34+
this.swap(array, index, i)
35+
index++
36+
}
37+
}
38+
this.swap(array, p, index - 1)
39+
return index - 1
40+
}
41+
42+
private swap(array: number[], p: number, q: number) {
43+
const temp = array[p]
44+
array[p] = array[q]
45+
array[q] = temp
46+
}
47+
}
48+
49+
const testFindSortNum = [4, 2, 5, 12, 3]
50+
const kthNum = new KthNum()
51+
const num = kthNum.getKthNum(testFindSortNum, 3)
52+
console.log(num)

typescript/12_sorts/quickSort.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* 快速排序是不稳定的排序
3+
* 原地排序,空间复杂度O(1),比归并排序使用更广泛
4+
* 平均复杂度基本接近O(nlg(n))
5+
*/
6+
interface ArraySort {
7+
sort(array: number[]): void
8+
}
9+
10+
class QuickSort implements ArraySort {
11+
sort(array: number[]): void {
12+
this.sortInternally(array, 0, array.length - 1)
13+
}
14+
15+
private sortInternally(array: number[], p: number, r: number) {
16+
if (p >= r) return
17+
// 获取分界点
18+
const q: number = this.partition(array, p, r)
19+
this.sortInternally(array, p, q - 1)
20+
this.sortInternally(array, q + 1, r)
21+
}
22+
23+
private partition(array: number[], p: number, r: number): number {
24+
/**
25+
* 参考值pivot,小于pivot的放在左边,大于pivot的在右边,最后再把分界点的值和它做交换
26+
* 这样返回的index一定是值在中间的下标
27+
*/
28+
const pivot = array[p]
29+
// 分界点
30+
let index = p + 1
31+
for (let i = index; i <= r; i++) {
32+
if (array[i] < pivot) {
33+
this.swap(array, index, i)
34+
// 找到了比标记值小的元素就移动分界点
35+
index++
36+
}
37+
}
38+
this.swap(array, p, index - 1)
39+
return index - 1
40+
}
41+
42+
private swap(array: number[], p: number, q: number) {
43+
const temp = array[p]
44+
array[p] = array[q]
45+
array[q] = temp
46+
}
47+
}
48+
49+
const testSort = [1, 3, 2, 3, 10, 9, 7, 6, 0, -12]
50+
const quickSort: ArraySort = new QuickSort()
51+
quickSort.sort(testSort)
52+
console.log(testSort)

0 commit comments

Comments
 (0)