分治思想:
(在其他地方看到的)
//快速排序
#include<iostream>
using namespace std;
void qsort(int *array, int len)
{
int value, start, end;
if (len <= 1) //边界条件
return;
value = array[0]; //
start = 0;
end = len - 1;
while (start < end)
{
for (; start < end; --end) //遍历
{
if (array[end] < value)
{
array[start++] = array[end];
break;
}
}
for (; start < end; ++start)
{
if (array[start] > value)
{
array[end--] = array[start];
break;
}
}
}
array[start] = value;
qsort(array, start);
qsort(array + start + 1, len - start - 1);
}
int main()
{
int array[] = { 1, 3, 5, 7, 9, 0, 2, 4, 6, 8 };
qsort(array, sizeof(array) / sizeof(array[0]));
for (int i = 0; i < sizeof(array) / sizeof(array[0]); i++)
{
cout << array[i] << " ";
}
cout << endl;
system("pause");
return 0;
}