#include<iostream>
using namespace std;
#define MIXSIZE 20
typedef int keyType;
typedef int infoType;
typedef struct {
keyType key;
infoType otherinfo;
}RedType;
typedef struct {
RedType r[MIXSIZE + 1];
int length;
}SqList;
/*void BubbleSort(SqList& L)//冒泡排序
{
RedType t;
int m = L.length - 1;//比较到倒数第二个数和最后一个数比较
int flag = 1;//flag用来标记某一趟是否发生了循环;
while ((m > 0) && flag == 1)//
{
flag = 0;//flag置为0,如果本此没有发生循环,则不会执行下一次。
for (int j = 1; j <= m; j++)//从第一个数开始比较
{
if (L.r[j].key > L.r[j + 1].key)
{
flag = 1;//本此发生了交换继续下次操作
t = L.r[j];
L.r[j] = L.r[j + 1];
L.r[j + 1] = t;
}
}
--m;
}
}*/
//快速排序
int Partition(SqList& L, int low, int high)
{
//对顺序表L中的子表r[low..high]进行一趟排序,返回枢轴位置
L.r[0] = L.r[low];//用子表的第一个记录做枢轴记录
int pivotkey = L.r[low].key;//枢轴记录关键字保存在pivotkey中
while (low < high)//从表的两端交替地向中间扫描
{
while (low < high && L.r[high].key >= pivotkey)
{
--high;
}
L.r[low] = L.r[high];//将比枢轴记录小的记录移到低端
while (low < high && L.r[low].key <= pivotkey)
{
++low;
}
L.r[high] = L.r[low];//将比枢轴记录大的记录移到高端
}
L.r[low] = L.r[0];//枢轴记录到位
return low;//返回枢轴位置
}
void Qsort(SqList& L, int low, int high)
{
//调用前置初值:low=1;high=L.length;
//对顺序表L中的字序列L.r[low...high]做出快速排序
if (low < high)
{
int pivotloc = Partition(L, low, high);//将L.r[low...high]一分为二,pivotloc枢轴位置
Qsort(L,low,pivotloc-1);
Qsort(L,pivotloc+1,high);
}
}
void QuickSort(SqList& L)//对顺序表L做出快速排序
{
Qsort(L, 1, L.length);
}
int main()
{
SqList L;
L.length = 8;
cout << "请输入数据";
for (int i = 1; i <= L.length; i++)//输入数据
{
cin >> L.r[i].key;
}
//没有排序之前的遍历
cout << "遍历数据";
for (int i = 1; i <= L.length; i++)
{
cout << L.r[i].key << " ";
}
cout << endl;
/*//BubbleSort(L);//冒泡排序
//冒泡排序后的遍历;
cout << "排序后的遍历";
for (int i = 1; i <= L.length; i++)
{
cout << L.r[i].key << " ";
}
cout << endl;*/
//快速排序
QuickSort(L);//
cout << "遍历排序后数据";
for (int i = 1; i <= L.length; i++)
{
cout << L.r[i].key << " ";
}
cout << endl;
return 0;
}