代码如下:
#include "stdafx.h"
#include<Windows.h>void quiksort(int a[], int low, int high)
{
int i = low;//开始
int j = high;//结束
int temp = a[i];//pivotkey
//一趟排序
if (low < high)
{
while (i < j)
{
while ((a[j] >= temp) && (i < j))
{
j--;
}
a[i] = a[j];//从右至左,大则交换
while ((a[i] <= temp) && (i < j))
{
i++;
}
a[j] = a[i];
}
a[i] = temp;
quiksort(a, low, i - 1);
quiksort(a, j + 1, high);
}
else
{
return;
}
}
int main()
{
int length,i,j;
int array[100];
scanf("%d", &length);
for (i = 0; i < length; i++) {
scanf("%d", &array[i]);
}
quiksort(array,0,length-1);
for (j = 0; j < length; j++) {
printf("%d ",array[j]);
}
Sleep(5000);
return 0;
}
开发环境:VS2015。