麻烦大家看一下,按照书上实现的快速排序,但一直输出的是排序前,这是为什么呢?应该该怎么修改呢?谢谢!
#include<iostream>
using namespace std;
typedef struct
{
int key1;
int key2;
}Data;
typedef struct
{
Data *r = NULL;
int length;
}SqList;
int point;
int Partition(SqList &L, int low, int high)
{
L.r[0] = L.r[low];
point = L.r[low].key1;
while (low < high)
{
while (low < high && L.r[high].key1 >= point)
{
--high;
}
L.r[low] = L.r[high];
while (low < high && L.r[low].key1 <= point)
{
++low;
}
L.r[high] = L.r[low];
}
L.r[low] = L.r[0];
return low;
}
void QSort(SqList &L, int low, int high)
{
if (low < high)
{
point = Partition(L, low, high);
QSort(L, low, point - 1);
QSort(L, point + 1, high);
}
for (int i = 0; i < 5; i++)
{
cout << L.r[i].key1;
}
}
int main(void)
{
SqList L;
L.r = new Data[10];
for (int i = 0; i < 5; i++)
{
cin >> L.r[i].key1;
}
QSort(L, 1, L.length);
delete[] L.r;
return 0;
}