33 * 原地排序,空间复杂度O(1),比归并排序使用更广泛
44 * 平均复杂度基本接近O(nlg(n))
55 */
6- interface ArraySort {
7- sort ( array : number [ ] ) : void
8- }
96
10- class QuickSort implements ArraySort {
11- sort ( array : number [ ] ) : void {
7+ export class QuickSort {
8+ static sort ( array : number [ ] ) : void {
129 this . sortInternally ( array , 0 , array . length - 1 )
1310 }
14-
15- private sortInternally ( array : number [ ] , p : number , r : number ) {
11+ private static sortInternally ( array : number [ ] , p : number , r : number ) {
1612 if ( p >= r ) return
1713 // 获取分界点
1814 const q : number = this . partition ( array , p , r )
1915 this . sortInternally ( array , p , q - 1 )
2016 this . sortInternally ( array , q + 1 , r )
2117 }
22-
23- private partition ( array : number [ ] , p : number , r : number ) : number {
18+ private static partition ( array : number [ ] , p : number , r : number ) : number {
2419 /**
2520 * 参考值pivot,小于pivot的放在左边,大于pivot的在右边,最后再把分界点的值和它做交换
2621 * 这样返回的index一定是值在中间的下标
@@ -39,14 +34,13 @@ class QuickSort implements ArraySort {
3934 return index - 1
4035 }
4136
42- private swap ( array : number [ ] , p : number , q : number ) {
37+ private static swap ( array : number [ ] , p : number , q : number ) {
4338 const temp = array [ p ]
4439 array [ p ] = array [ q ]
4540 array [ q ] = temp
4641 }
4742}
4843
4944const testSort = [ 1 , 3 , 2 , 3 , 10 , 9 , 7 , 6 , 0 , - 12 ]
50- const quickSort : ArraySort = new QuickSort ( )
51- quickSort . sort ( testSort )
45+ QuickSort . sort ( testSort )
5246console . log ( testSort )
0 commit comments