#include<stdio.h>
#include<windows.h>
void print(int array[], int size) //打印函数
{
for (int i = 0; i < size; i++)
{
printf("%d ", array[i]);
}
}
int partion2(int array[], int left, int right) //找基准值函数
{
int begin = left;
int end = right;
int provite = array[right];
while (begin < end)
{
while (begin < end&&array[begin] <= provite)
{
begin++;
}
array[end] = array[begin];
while (begin < end&&array[end] >= provite)
{
end--;
}
array[begin] = array[end];
}
array[begin] = provite;
return begin;
}
void quicksort(int array[], int left, int right)
{
if (left >= right) //判定一下需不需要排序
{
return;
}
int div = partion2(array, left, right);
quicksort(array, left, div - 1); //先递归左边区间
quicksort(array, div+1, right ); //再递归右边区间
}
int main()
{
int arr[] = { 2, 6, 7, 5, 9, 4, 3, 1, 0, 10 };
int size = sizeof(arr) / sizeof(int);
printf("排序前:\n");
print(arr, size);
printf("\n排序后:\n");
quicksort(arr, 0, size - 1);
print(arr, size);
system("pause");
return 0;
}
快速排序---挖坑法
最新推荐文章于 2022-10-03 20:57:21 发布