快速排序算法练习

快速排序算法练习
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]);
    }
}



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值