#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;
}
标签:int,quicksort,pos,high,while,low,排序,快速
From: https://blog.csdn.net/weixin_73598089/article/details/140457893