#include <iostream>
using namespace std;
void quick_sort(int *A, int n)
{
int i = 0, j = n - 1;
if(n <= 1)
return ;
int key = A[0];
int sign = 0;
while(i != j)
{
if(sign == 0)
{
if(A[j] < key)
{
int temp = A[j];
<span style="color:#ff0000;"> A[j] = key;//key 写成A[0] 呵呵
A[i++] = temp;//开始竟然写成A[i++] = key</span>
sign = 1;
}
else
j--;
}
else
{
if(A[i] > key)
{
int temp = A[i];
<span style="color:#ff0000;"> A[i] = key; //key写成A[0]
A[j--] = temp; //temp 写成key</span>
sign = 0;
}
<span style="color:#ff0000;"> else //这个分支竟然没写!!!
i++;</span>
}
}
quick_sort(A, i);
quick_sort(A + i + 1, n - i - 1); //数组大小搞错了,搞成i - 1, n - i + 1 晕!!!
}
int main()
{
int xx[] = {3, 4 , 2, 11, 123, 10, 102, 13};
quick_sort(xx, 8);
for(int i = 0; i < 8; i++)
cout << "i:" << xx[i] << endl;
return 0;
}
有什么好说的,难怪面程序题思路都知道,就是写不对!活该!!!
上面的quicksort是不适用额外空间的,简单高效