快速排序,写了一个函数和测试程序
#include <iostream>
int QSort (int * pList, int low, int high)
{
int low_old = low;
int high_old = high;
while (low < high)
{
int pivot_value = pList[low];
while ( low < high && pList[high] >= pivot_value)
{
--high;
}
{
int tmp = pList[low];
pList[low] = pList[high];
pList[high] = tmp;
}
while (low < high && pList[low] <= pivot_value)
{
++low;
}
int tmp = pList[low];
pList[low] = pList[high];
pList[high] = tmp;
}
if (low_old < high_old)
{
QSort (pList, low_old, low - 1);
QSort (pList, low + 1, high_old);
}
return 0;
}
int main (int argc, char * argv[])
{
int array_1[] = {5, 8, 4, 2, 9, 3, 34, 22, 12};
QSort (array_1, 0, 8);
for (int i = 0; i < 9; ++i)
{
std::cout << array_1[i] << " ";
}
system ("pause");
return 0;
}