void swap(int& a, int& b)
{
int c = a;
a = b;
b = c;
}
int partion(int a[], int p, int r)
{
int i = p-1;
int x = a[r];
int j ;
for (j = p; j <= r-1; ++j)
{
if (a[j] <= x)
{
++i;
swap(a[i], a[j]);
}
}
swap(a[i+1], a[r]);
return i + 1;
}
void quicksort(int a[], int p, int r)
{
if (p < r)
{
int q = partion(a, p, r);
quicksort(a,p, q-1);
quicksort(a,q+1, r);
}
}
int main()
{
int a[7] = {7,6,5,1,3,2,4};
quicksort(a, 0, 6);
for (int i = 0; i < 7; ++i)
{
cout << a[i] << " ";
}
cout <<endl;
return 0;
}
posted on 2008-03-07 14:34
RUI 阅读(197)
评论(0) 编辑 收藏 引用