19. 快速排序

要求根据给定输入,按照课堂给定的快速排序算法进行排序,输出排序结果和median3的返回值。
midian3是指从头尾和中间取3个元素,将头部元素和3个元素中大小的中间值交换,以避免选出最大元素或者最小元素的情况出现。
注:1,cutoff值为5,元素个数不足cutoff使用插入排序。
2,输入、输出格式参见测试用例。

测试用例:

用例1:
输入:
41
17
34
0
19
#
输出:
After Sorting:
0 17 19 34 41
Median3 Value:
none
用例2:
输入:
61
59
82
-10
31
-2
-3
10
2
108
12
80
-21
127
12
#
输出:
After Sorting:
-21 -10 -3 -2 2 10 12 12 31 59 61 80 82 108 127
Median3 Value:
12 -3 61

代码

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int median3[102], cnt = 0;
int numVector[10002];

/*此函数在表左端、右端和中间点三者取中值,交换到右端*/
int _Median3(int Left, int Right)
{
    int Middle = (Left + Right) / 2;//写成Right+(Left-Right)/2时WA了
    if (numVector[Left] > numVector[Middle])
        swap(numVector[Left], numVector[Middle]);
    if (numVector[Left] > numVector[Right])
        swap(numVector[Left], numVector[Right]);
    if (numVector[Middle] > numVector[Right])
        swap(numVector[Middle], numVector[Right]);
    swap(numVector[Middle], numVector[Right - 1]);
    median3[cnt++] = numVector[Right - 1];
    return numVector[Right - 1];
}

void InsertSort(int Left, int Right)
{
    for (int i = Left + 1; i <= Right; i++)
    {
        if (numVector[i] - numVector[i - 1] < 0)
        {
            int j = Left, tmp = numVector[i];//
            for (; numVector[j] < numVector[i]; j++);
            for (int k = i; k >= j + 1; k--)
                numVector[k] = numVector[k - 1];
            numVector[j] = tmp;//写成numVector[j]=numVector[i]就会出错  ???? .因为前面循环很明显对numVector[i]有改动啊
        }
    }
}

int Partation(int Left, int Right)
{
    if (Right - Left < 5)//元素个数不足cutoff=5使用插入排序
    {
        InsertSort(Left, Right);
        return -1;
    }
    else
    {
        int pivot = _Median3(Left, Right);
        int  R = Right - 1, L = Left;
        while (L < R)
        {
            while (L < R&&numVector[++L] < pivot);//小于或等于pivot停步
            while (L < R&&numVector[--R] > pivot);//大于或等于pivot停步
            swap(numVector[L], numVector[R]);
        }
        swap(numVector[L], numVector[Right - 1]);
        return L;
    }
}

void QuickSort(int Left, int Right)
{
    if (Left < Right)
    {
        int Middle = Partation(Left, Right);//划分
        if (Middle != -1)
        {
            QuickSort(Left, Middle - 1);//对左侧子序列快排
            QuickSort(Middle + 1, Right);//对右侧子序列快排
        }
    }
}

int main()
{
    int T = 0;
    string st;
    while(1)
    {
        cin >> st;
        if (st.at(0) == '#')
            break;
        numVector[T++] = atoi(st.c_str());
    }
    QuickSort(0, T - 1);//快排
    cout << "After Sorting:" << endl;
    for (int j = 0; j < T; j++)
        cout << numVector[j] << " ";
    cout << endl;
    cout << "Median3 Value:" << endl;
    if (T > 5)
    {
        for (int j = 0; j < cnt; j++)
            cout << median3[j] << " ";
    }
    else
        cout << "none";
    cout << endl;

    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值