代码实现:
#include <iostream>
using namespace std;
//快速排序
void QuickSort(int *arr, int len, int left, int right)
{
int i, j, t, tmp;
if (left > right)
{
return;
}
tmp = arr[left];
i = left;
j = right;
while (i != j)
{
while (arr[j] >= tmp&&i < j)
{
j--;
}
while (arr[i] <= tmp&&i < j)
{
i++;
}
if (i < j)//交换
{
t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
arr[left] = arr[i];
arr[i] = tmp;
QuickSort(arr,len,left, i - 1);
QuickSort(arr,len,i + 1, right);
}
//打印
void Show(int *arr, int len)
{
for (int i = 0; i < len; i++)
{
cout << arr[i] << " ";
}
}
int main()
{
int arr[] = { 1, 45, 20, 87, 96, 8, 89, 0, 34, 12, 23 };
int len = sizeof(arr) / sizeof(arr[0]);
QuickSort(arr, len, 0, len-1);
Show(arr, len);
return 0;
}
运行结果: