快速排序算法练习
Time Limit: 1000 MS Memory Limit: 32768 K
Total Submit: 352 (219 users) Total Accepted: 245 (217 users) Special Judge: No
Description
利用快速排序算法实现递增排序。
Input
输入一组整数
Output
输出递增的整数序列
Sample Input
2 1 5 4 3
Sample Output
1 2 3 4 5
#include<stdio.h>
#include<bits/stdc++.h>
#include<iostream>
#include<stdlib.h>
#define maxn 1005
#define ElemType int
#define OK 1
using namespace std;
typedef struct
{
ElemType key;
} RcdType;
typedef struct
{
ElemType *elem;
int length;
int listsize;
} sqlist;
sqlist start()
{
sqlist l;
l.elem=(int *)malloc(maxn *sizeof(ElemType));
l.length=0;
l.listsize=maxn;
return l;
}
void insert2 (sqlist &l,ElemType e)
{
l.elem[l.length+1]=e;
l.length++;
}
int Partition (sqlist &L,int low,int high)
{
L.elem[0]=L.elem[low];
int pivotekey=L.elem[low];
while(low<high)
{
while(low<high&&L.elem[high]>=pivotekey)
--high;
L.elem[low]=L.elem[high];
while(low<high&&L.elem[low]<=pivotekey)
++low;
L.elem[high]=L.elem[low];
}
L.elem[low]=L.elem[0];
return low;
}
void 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);
}
}
void QuickSort(sqlist &L)//快速排序
{
QSort(L,1,L.length);
}
int main()
{
sqlist l;
l=start();
int a;
while(~scanf("%d",&a))
{
insert2(l,a);
}
QuickSort(l);
for(int i=1;i<l.length+1;i++)
{
printf("%d ",l.elem[i]);
}
}