快速排序
默认为输入5个数进行排序
快排讲究的主要是分的过程
#include <stdio.h>
int QucikSort1(int *a, int low, int high);
void QuickSort(int *a, int low, int high);
int main()
{
int a[6]; //a[0]用来保存进行排序的数
printf("Please input five numbers as a space separated:");
for (int i = 1; i <= 5; i++)
scanf("%d", a + i);
printf("before sort:");
for (int i = 1; i <= 5; i++)
printf("%d ", *(a + i));
QuickSort(a, 1, 5);
printf("\nafter sort:");
for (int i = 1; i <= 5; i++)
printf("%d ", *(a + i));
printf("\n");
return 0;
}
int QuickSort1(int *a, int low, int high) //核心部分
{
int pivotkey;
pivotkey = a[low];
a[0] = a[low];
while (low < high)
{
while (low < high && a[high] >= pivotkey)
high--;
a[low] = a[high];
while (low < high && a[low] <= pivotkey)
low++;
a[high] = a[low];
}
a[low] = a[0];
return low;
}
void QuickSort(int *a, int low, int high)
{
int pivotloc;
if (low < high) //low < high时,停止递归
{
pivotloc = QuickSort1(a, low, high);
QuickSort(a, low, pivotloc - 1); //递归解决分的部分
QuickSort(a, pivotloc + 1, high);
}
}