Skip to content

Commit 7b6b85c

Browse files
committed
override quickSort cpp solution
override quickSort cpp solution
1 parent 35de10a commit 7b6b85c

File tree

1 file changed

+45
-25
lines changed

1 file changed

+45
-25
lines changed

6.quickSort.md

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -157,31 +157,51 @@ func swap(arr []int, i, j int) {
157157

158158

159159
```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-
}
160+
void exchangeVecValue(vector<int>::iterator t1, vector<int>::iterator t2)
161+
{
162+
int temp = *t1;
163+
*t1 = *t2;
164+
*t2 = temp;
165+
}
166+
//快速排序分区函数
167+
int QuickSortPart(vector<int> *vec, int left, int right)
168+
{
169+
int pivot = left;
170+
int index = pivot + 1;
171+
172+
for (int i = left + 1; i < right; i++)
173+
{
174+
if ((*vec)[i] < (*vec)[pivot])
175+
{
176+
exchangeVecValue((*vec).begin()+i, (*vec).begin()+index);
177+
index++;
178+
}
179+
}
180+
index--;
181+
exchangeVecValue((*vec).begin() + pivot, (*vec).begin() + index);
182+
183+
return index;
184+
}
185+
//快速排序递归函数
186+
void QuickSortA(vector<int> *vec, int left, int right)
187+
{
188+
if(right>left)
189+
{
190+
//patition分区
191+
int pivot = QuickSortPart(vec, left, right);
192+
//左排序
193+
QuickSortA(vec, left, pivot - 1);
194+
//右排序
195+
QuickSortA(vec, pivot + 1, right);
196+
}
197+
}
198+
199+
//快速排序母函数
200+
vector<int> AlgorithmSort::QuickSort(vector<int> vec)
201+
{
202+
QuickSortA(&vec,0,vec.size()-1);
203+
return vec;
204+
}
185205
```
186206
187207
## 7. Java 代码实现

0 commit comments

Comments
 (0)