数据结构-排序-快速排序

注:本题只是单趟快排 

Description
对输入的关键字进行快速排序,输出第一趟的排序结果。
说明:输入第一行为关键字数;第二行为关键字序列
输出为第一趟快速排序的结果。
Sample Input
8
49 38 65 97 76 13 27 49

Sample Output

27 38 13 49 76 97 65 49

Hint
输出有换行
#include <iostream>
#include <bits/stdc++.h>
#define Maxnum 105
#define padix 10
using namespace std;

typedef struct
{
    int key;
    int arr[Maxnum];
} keynode;
typedef struct
{
    keynode *R;
    int length;
} Sqlist;
void initL(Sqlist &L)
{
    L.R = new keynode[Maxnum];
}
void creatL(Sqlist &L)
{
    int m;
    cin >> m;
    L.length = m;
    for (int i = 1; i <= m; i++)
    {
        cin >> L.R[i].key;
    }
}
void Partition(Sqlist &L, int low, int high)
{ // 单趟快速排序
    L.R[0] = L.R[low];
    int pivo = L.R[low].key;
    while (low < high)
    {
        if (low < high && L.R[high].key >= pivo)
        { // 从右往前找小的交换(带等号)
            high--;
        }
        L.R[low] = L.R[high];
        if (low < high && L.R[low].key <= pivo)
        {
            low++;
        }
        L.R[high] = L.R[low];
    }
    L.R[low] = L.R[0]; // 结束为相等也是插入位置
}
void PrintL(Sqlist L)
{
    int cnt = 0;
    for (int i = 1; i <= L.length; i++)
    {
        cout << L.R[i].key;
        if (cnt != L.length - 1)
        {
            cout << " ";
        }
        cnt++;
    }
    cout << "\n";
}
int main()
{
    Sqlist L;
    initL(L);
    creatL(L);
    Partition(L, 1, L.length);
    PrintL(L);
    return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值