#include<iostream>
using namespace std;
int partition(int a[], int low, int high)
{
int pivot = a[low];
while (low < high)
{
while (low < high && a[high] >= pivot)//先从high开始
high--;
a[low] = a[high];
while (low < high && a[low] <= pivot)
low++;
a[high] = a[low];
}
a[low]=pivot;
return low;
}
void quicksort(int a[], int low, int high)
{
if (low < high)
{
int pos=partition(a, low, high);
quicksort(a, low, pos - 1);
quicksort(a, pos + 1, high);
}
}
int main()
{
int arr[5] = { 10,0,1,8,45 };
quicksort(arr, 0, 4);
cout << "排序之后:" << endl;
for (int i = 0; i < 5; i++)
{
cout << arr[i] << " ";
}
return 0;
}
快速排序quicksort
最新推荐文章于 2024-08-13 23:31:35 发布