Skip to content

Commit 360f343

Browse files
authored
update lagyout
唔,其实我想表达的重点是优化掉swap这个交换,这里(swap)会增加同排序数量级(O(nlgn))的比较次数和交换次数操作,有点浪费:)
1 parent 91c9c35 commit 360f343

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

6.quickSort.md

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,31 +62,30 @@ function swap(arr, i, j) {
6262
arr[i] = arr[j];
6363
arr[j] = temp;
6464
}
65-
66-
//快排2 基于严版改为js版.可以减少swap的操作过程.
67-
function paritition2(arr,low,high){
68-
let pivot=arr[low];
69-
while(low<high){
70-
while(low<high && arr[high] >pivot){
71-
--high;
72-
}
73-
arr[low]=arr[high];
74-
while(low<high && arr[low]<=pivot){
75-
++low;
76-
}
77-
arr[high]=arr[low];
65+
functiion paritition2(arr, low, high) {
66+
let pivot = arr[low];
67+
while (low < high) {
68+
while (low < high && arr[high] > pivot) {
69+
--high;
70+
}
71+
arr[low] = arr[high];
72+
while (low < high && arr[low] <= pivot) {
73+
++low;
7874
}
79-
arr[low]=pivot;
80-
return low;
75+
arr[high] = arr[low];
8176
}
77+
arr[low] = pivot;
78+
return low;
79+
}
8280

83-
function quickSort2(arr,low,high){
84-
if (low < high) {
85-
let pivot = this.paritition2(arr, low, high);
86-
this.quickSort2(arr, low, pivot - 1);
87-
this.quickSort2(arr, pivot + 1, high);
88-
}
89-
81+
function quickSort2(arr, low, high) {
82+
if (low < high) {
83+
let pivot = this.paritition2(arr, low, high);
84+
this.quickSort2(arr, low, pivot - 1);
85+
this.quickSort2(arr, pivot + 1, high);
86+
}
87+
return arr;
88+
}
9089

9190
```
9291

0 commit comments

Comments
 (0)