1.快速排序:
通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
2.代码:
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
//打印数组
void display(int array[], int maxnum)
{
int i;
for (i = 0; i < maxnum; i++) {
printf("%d, ", array[i]);
}
printf("\n");
}
void quickSort(int array[], int l, int r)
{
int i, j;
int temp;
if (l >= r)return;//只有一个数或者没有数就不用排序了
i = l;
j = r;
temp = array[i];
while (i != j)
{
while (i < j&&array[j] >= temp)
j--;
if (i < j)array[i++] = array[j];
while (i < j&&array[i] <= temp)
i++;
if (i < j)array[j--] = array[i];
}
array[i] = temp;
quickSort(array, l, i - 1);
quickSort(array, i + 1, r);
}
int main() {
int max = 10;
int array[10] = { 0,0,2,3,99,55,44,6,2,5 };
quickSort(array, 0, max - 1);
display(array, max);
system("pause");
return 0;
}
3.结果:
