#include <iostream>
using namespace std;
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int a[], int left, int right)
{
int i = left-1;
int j = right;
int pivot = a[right];
while (i < j)
{
while (a[++i] < pivot)
{
}
while (a[--j] > pivot)
{
if (left == j)
{
break;
}
}
if (i < j)
{
swap(a[i], a[j]);
}
}
swap(a[i], a[right]);
return i;
}
void QuickSort(int a[], int left, int right)
{
if (left >= right)
{
return ;
}
int pivot = partition(a, left, right);
QuickSort(a, left, pivot-1);
QuickSort(a, pivot+1, right);
}
int main()
{
int a[] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
QuickSort(a, 0, 9);
for (int i=0; i<10; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
system("pause");
return 0;
}
快速排序
最新推荐文章于 2021-11-19 16:57:43 发布