#include <iostream>
using namespace std;
int Partition(int *a, int bNum, int eNum)
{
int tem = a[eNum];
int i=bNum-1, j;
for (j = bNum; j < eNum; j++)
{
if (a[j] < tem)
{
i = i + 1;
int tem1 = a[j];
a[j] = a[i];
a[i] = tem1;
}
}
int tem2 = a[i + 1];
a[i + 1] = a[eNum];
a[eNum] = tem2;
return i + 1;
}
void QuickSort(int *a, int bNum, int eNum)
{
if (bNum < eNum)
{
int q = Partition(a, bNum, eNum);
QuickSort(a, bNum, q - 1);
QuickSort(a, q+1, eNum);
}
}
int main()
{
int n=14, i;
int a[14] = {22,44,5,33,22,4,404,505,1,99,2,88,36,89};
cout << "The input array is : "<<endl;
for (i = 0; i < n; i++)
cout << a[i] << " ";
QuickSort(a,0,n-1);
cout << "The array after sort is :" << endl;
for (i = 0; i < n; i++)
cout << a[i] << " ";
system("pause");
}
快速排序
最新推荐文章于 2024-09-07 14:53:08 发布