Skip to content

Commit 2939241

Browse files
wagnlinzhhustcc
authored andcommitted
Update 6.quickSort.md (#6)
* Update 6.quickSort.md update 基于严蔚敏版的js快排, C++版.优化掉swap的操作 * Update 6.quickSort.md * update lagyout 唔,其实我想表达的重点是优化掉swap这个交换,这里(swap)会增加同排序数量级(O(nlgn))的比较次数和交换次数操作,有点浪费:) * Update 6.quickSort.md
1 parent 523336f commit 2939241

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

6.quickSort.md

+56
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,31 @@ function swap(arr, i, j) {
6262
arr[i] = arr[j];
6363
arr[j] = temp;
6464
}
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;
74+
}
75+
arr[high] = arr[low];
76+
}
77+
arr[low] = pivot;
78+
return low;
79+
}
80+
81+
function quickSort2(arr, low, high) {
82+
if (low < high) {
83+
let pivot = paritition2(arr, low, high);
84+
quickSort2(arr, low, pivot - 1);
85+
quickSort2(arr, pivot + 1, high);
86+
}
87+
return arr;
88+
}
89+
6590
```
6691

6792

@@ -127,3 +152,34 @@ func swap(arr []int, i, j int) {
127152
arr[i], arr[j] = arr[j], arr[i]
128153
}
129154
```
155+
156+
## 6. C++版
157+
158+
159+
```C++
160+
//严蔚敏《数据结构》标准分割函数
161+
Paritition1(int A[], int low, int high) {
162+
int pivot = A[low];
163+
while (low < high) {
164+
while (low < high && A[high] >= pivot) {
165+
--high;
166+
}
167+
A[low] = A[high];
168+
while (low < high && A[low] <= pivot) {
169+
++low;
170+
}
171+
A[high] = A[low];
172+
}
173+
A[low] = pivot;
174+
return low;
175+
}
176+
177+
void QuickSort(int A[], int low, int high) //快排母函数
178+
{
179+
if (low < high) {
180+
int pivot = Paritition1(A, low, high);
181+
QuickSort(A, low, pivot - 1);
182+
QuickSort(A, pivot + 1, high);
183+
}
184+
}
185+
```

0 commit comments

Comments
 (0)