|
@@ -4,11 +4,14 @@
|
|
#include<time.h>
|
|
#include<time.h>
|
|
void QuickSort(int* arr, int left, int right)
|
|
void QuickSort(int* arr, int left, int right)
|
|
{
|
|
{
|
|
|
|
+ //直到不合理情况出现结束递归
|
|
if (left >= right)
|
|
if (left >= right)
|
|
{
|
|
{
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
+ //找到"left"对应在数组的位置,并将保存位置的序号赋给position
|
|
int position = PartSort(arr, left, right);
|
|
int position = PartSort(arr, left, right);
|
|
|
|
+ //分别对position左右进行进行快速排序
|
|
QuickSort(arr, left, position - 1);
|
|
QuickSort(arr, left, position - 1);
|
|
QuickSort(arr, position + 1, right);
|
|
QuickSort(arr, position + 1, right);
|
|
}
|
|
}
|
|
@@ -17,7 +20,7 @@ void QuickSort(int* arr, int left, int right)
|
|
int PartSort(int* arr, int left, int right)
|
|
int PartSort(int* arr, int left, int right)
|
|
{
|
|
{
|
|
int key = arr[left];
|
|
int key = arr[left];
|
|
-
|
|
|
|
|
|
+ //找到"left"的位置,并保证左边比其小,右边比其大
|
|
while (left < right) {
|
|
while (left < right) {
|
|
while (left < right && arr[right] >= key)
|
|
while (left < right && arr[right] >= key)
|
|
{
|
|
{
|
|
@@ -31,7 +34,7 @@ int PartSort(int* arr, int left, int right)
|
|
}
|
|
}
|
|
swap(arr, left, right);
|
|
swap(arr, left, right);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ //将找出的最终位置返回
|
|
return left;
|
|
return left;
|
|
}
|
|
}
|
|
|
|
|