#include
using namespace std;
int adjustArray(int s[], int l, int r)
{
int i = l, j = r, x = s[i];
while (i
while (i < j&&s[j] >= x)
j--;
if (i < j)
{
s[i] = s[j];
i++;
}
while (i < j&&s[i] < x)
i++;
if (i < j)
{
s[j] = s[i];
j--;
}
}
s[i] = x;
int mid = i;
return mid;
}
void quick_sort(int s[], int l, int r)
{
if (l < r)
{
int mid = adjustArray(s,l,r);
quick_sort(s, l, mid - 1);
quick_sort(s,mid+1,r);
}
}
void main()
{
int a[15] = {1,4,2,8,5,77,66,44,0,6,3,8,7,55,44};
//int a = 4, b = 9;
quick_sort(a, 0, 14);
for (int i = 0; i < 15;i++)
{
cout << a[i] << endl;
}
system("pause");
}
原文见下:
白话经典算法系列之六 快速排序 快速搞定