您的位置:首页 > 其它

快速排序

2016-04-28 16:16 218 查看
#include "stdafx.h"

void PrintFunc(int a[], int n)
{
for (int i = 0; i < n;i++)
{
printf("%d ", a[i]);
}

printf("\n");
}

//快速排序
int QSort(int a[], int n)
{
int low = 0;
int high = n - 1;
int tmp, j;

while (low < high)
{
for (j = low; j < high;++j)
{
if (a[j+1]<a[j])
{
tmp = a[j];
a[j] = a[j +1];
a[j + 1] = tmp;
}
}

high--;

for (j = high;j>low; --j)
{
if (a[j]<a[j-1])
{
tmp = a[j];
a[j] = a[j - 1];
a[j - 1] = tmp;
}
}

++low;

PrintFunc(a, n);
}

return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
int a[] = {9,8,7,6,5,4,3,2,1, 0};

//InsertSort(a, 10);
QSort(a, 10);
//PrintFunc(a, 10);
return 0;
}


Out:

E:\Debug>AlgoTest.exe
0 8 7 6 5 4 3 2 1 9
0 1 7 6 5 4 3 2 8 9
0 1 2 6 5 4 3 7 8 9
0 1 2 3 5 4 6 7 8 9
0 1 2 3 4 5 6 7 8 9
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: