快排算法具有O(n*log(n))的时间复杂性,以前经常直接调用c++里面的函数,覆写一下比较函数cmp,就实现快排算法,现在打算用递归算法来实现一下,话不多说,直接上代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void QuickSort(int *A, int left, int right)
{
if (left >= right)
return;
int x = A[(left + right) >> 1];
int low = left, high = right;
while (low < high)
{
while (A[low] < x)
low++;
while (A[high]>x)
high--;
if (low <= high)
{
int temp = A[low];
A[low] = A[high];
A[high] = temp;
low++;
high--;
}
}
QuickSort(A, left, high);
QuickSort(A, low, right);
}
int main()
{
int length, i, r[10000];
scanf("%d", &length);
for (i = 1; i <= length; i++)
scanf("%d", &r[i]);
QuickSort(r, 1, length);
for (i = 1; i <= length; i++)
printf(" %d", r[i]);
printf("\n");
system("pause");
return 0;
}