Description
利用快速排序算法实现递增排序。
Input
输入三组整数
Output
输出递增的整数序列
Sample Input
2 1 5 4 3 6 7
54 45 63 21 15
100 50 45 60 20 10
Sample Output
1 2 3 4 5 6 7
15 21 45 54 63
10 20 45 50 60 100
//快速排序算法
#include<bits/stdc++.h>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 10 // 线性表存储空间的初始分配量
#define LISTINCREMENT 2 // 线性表存储空间的分配增量
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#include<string.h>
#include<malloc.h>
#define MAXSIZE 20
#define EQ(a,b) ((a)==(b))
#define LT(a,b) ((a)< (b))
#define OVERFLOW -1
typedef int InfoType;
typedef int ElemType;
typedef int KeyType;
typedef int Status;
typedef struct
{
KeyType key;
InfoType otherinfo;
} RedType;
typedef struct
{
ElemType *elem;
int length;
int listsize;
} SqList;
int Insertsort(SqList &L)
{
int i,j;
for(i=1;i<=L.length-1;++i){
if(LT(L.elem[i],L.elem[i-1]))
{
L.elem[0]=L.elem[i];
L.elem[i]=L.elem[i-1];
for(j=i-2;LT(L.elem[0],L.elem[j]);--j)
L.elem[j+1]=L.elem[j];
L.elem[j+1]=L.elem[0];
}
}
}
int BubbleSort(SqList &L,int n)
{
int i = 0;
int j = 0;
for(i=0; i<n-1; i++)
{
for(j=n-1; j>i-1; j--)
{
if(L.elem[j]>L.elem[j+1])
{
int tmp = L.elem[j];
L.elem[j] = L.elem[j+1];
L.elem[j+1] = tmp;
}
}
}
}
Status InitList(SqList &L)
{
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem)
exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}
Status ListInsert(SqList &L,int i,ElemType e)
{
ElemType *newbase,*q,*p;
if(i<1||i>L.length+1)
return ERROR;
if(L.length>=L.listsize)
{
newbase=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase)
exit(OVERFLOW);
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]); p>=q; --p)
*(p+1)=*p;
*q=e;
++L.length;
return OK;
}
Status ListTraverse(SqList L,void(*vi)(ElemType&))
{
ElemType *p;
int i;
p=L.elem;
for(i=1; i<=L.length; i++)
if(i==1){
*p++;
}
else{
vi(*p++);
}
printf("\n");
return OK;
}
void visit(ElemType &c)
{
cout<<c<<' ';
}
int Partition(SqList &L,int low,int high)
{
L.elem[0]=L.elem[low];
int pivotkey=L.elem[low];
while(low<high)
{
while(low<high&&L.elem[high]>=pivotkey)
--high;
L.elem[low]=L.elem[high];
while(low<high&&L.elem[low]<=pivotkey)
++low;
L.elem[high]=L.elem[low];
}
L.elem[low]=L.elem[0];
return low;
}
int QSort(SqList &L,int low,int high)
{
if(low<high)
{
int pivotloc=Partition(L,low,high);
QSort(L,low,pivotloc-1);
QSort(L,pivotloc+1,high);
}
}
int QuickSort(SqList &L)
{
QSort(L,1,L.length-1);
}
int SelectMinKey(SqList L,int i)
{
int j,k;
k=i;
for(j=i;j<L.length;j++)
{
if(L.elem[k]>L.elem[j])
k=j;
}
return k;
}
int SelectSort(SqList &L)
{
int i,j;
for(i=1;i<L.length;i++)
{
j=SelectMinKey(L,i);
if(i!=j)
{
int temp=L.elem[i];
L.elem[i]=L.elem[j];
L.elem[j]=temp;
}
}
}
int main()
{
int j,k;
SqList L,Q,W;
ElemType e,e0;
Status i;
i=InitList(L);
i=InitList(Q);
i=InitList(W);
ListInsert(L,1,0);
ListInsert(W,1,0);
ListInsert(Q,1,0);
int n=0;
int ans;
j=1;
while(cin>>ans)
{
ListInsert(L,++j,ans);
n++;
if(cin.get()=='\n')
break;
}
QuickSort(L);
for(int i=1;i<n;i++)
{
cout<<L.elem[i]<<" ";
}
cout<<L.elem[n];
cout<<endl;
j=1;n=0;
while(cin>>ans)
{
ListInsert(Q,++j,ans);
n++;
if(cin.get()=='\n')
break;
}
QuickSort(Q);
for(int i=1;i<n;i++)
{
cout<<Q.elem[i]<<" ";
}
cout<<Q.elem[n];
cout<<endl;
j=1;n=0;
while(cin>>ans)
{
ListInsert(W,++j,ans);
n++;
if(cin.get()=='\n')
break;
}
QuickSort(W);
for(int i=1;i<n;i++)
{
cout<<W.elem[i]<<" ";
}
cout<<W.elem[n];
}