给一个无序表,使用快速排序算法对它进行排序。
函数接口定义:
int Partition(SqList &L,int low,int high);
void QuickSort(SqList &L, int low, int high);
其中L是待排序表,low和high是排序的区间。
裁判测试程序样例:
#include <iostream>
using namespace std;
#define MAXSIZE 50
typedef int KeyType;
typedef struct
{ KeyType key;
} ElemType;
typedef struct
{ ElemType r[MAXSIZE+1];
int length;
} SqList;
void Create(SqList &L)
{ int i;
cin>>L.length;
for(i=1;i<=L.length;i++)
cin>>L.r[i].key;
}
void Output(SqList L)
{ int i;
for(i=1;i<=L.length;i++)
cout<<L.r[i].key<<" ";
cout<<endl;;
}
int Partition(SqList &L,int low,int high);
void QuickSort(SqList &L, int low, int high);
int main ()
{ SqList L; int low,high;
Create(L);
low=1; high=L.length;
QuickSort(L,low,high);
Output(L);
return 0;
}
/* 请在这里填写答案 */
输入样例:
在这里填写一组输入
5
3 1 9 5 7
结尾无空行
输出样例:
1 3 5 7 9
结尾无空行
输入样例:
在这里填写一组输入
5
0 -1 8 -1 2
结尾无空行
输出样例:
-1 -1 0 2 8
结尾无空行
int Partition ( SqList &L,int low, int high ){
L.r[0].key=L.r[low].key;
int pivotkey=L.r[0].key;
while(low<high){
while(low<high&&L.r[high].key>=pivotkey) high--;
L.r[low].key=L.r[high].key;
while(low<high&&L.r[low].key<=pivotkey) low++;
L.r[high].key=L.r[low].key;
}
L.r[low].key=L.r[0].key;
return low;
}
void QuickSort ( SqList &L,int low, int high )
{ int pivotloc;
if(low<high)
{
pivotloc = Partition(L, low, high ) ;
QuickSort (L, low, pivotloc-1) ;
QuickSort (L, pivotloc+1, high );
}
}