Aizu - ALDS1_6_C Quick Sort

这个题写快排的时候必须按照题目中的思路走,要不然会WA(别问我怎么知道的o(╥﹏╥)o)

#include<cstdio>
#include<stack>
#include<queue>
#include<cmath>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#define TP 233333333333333
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<char, int> P2;
const int maxn = 1000005;
int n;
P2 arr[maxn], arrcmp[maxn];
void quickSort(P2 arr[], int low, int high);
void mergeSort(P2 arr[], P2 temp[], int low, int high);
void checker();
int main(void) {
    cin >> n;
    getchar();
    for (int i = 0; i<n; i++) {
        scanf("%c %d", &arr[i].first, &arr[i].second);
        getchar();
        arrcmp[i] = arr[i];
    }
    quickSort(arr, 0, n-1); \\快排排元素
    P2* t = new P2 [n+5];
    mergeSort(arrcmp, t, 0, n-1); \\归并再排一遍
    delete [] t;
    checker(); \\对比归并和快排的结果
    for (int i = 0; i<n; i++)
        printf("%c %d\n",arr[i].first, arr[i].second);
    return 0;
}
void quickSort(P2 arr[], int low, int high) {
    if (low >= high)
        return;
    int kase = low;
    for (int i = low; i<high; i++) 
        if (arr[i].second <= arr[high].second)
            swap(arr[kase++], arr[i]);
    swap(arr[kase], arr[high]);
    quickSort(arr, low, kase-1);
    quickSort(arr, kase+1, high);
}
void mergeSort(P2 arr[], P2 temp[], int low, int high) {
    if (low >= high)
        return;
    int len = high-low, mid = len/2 + low;
    int start1 = low, end1 = mid, start2 = mid+1, end2 = high;
    mergeSort(arr, temp, start1, end1);
    mergeSort(arr, temp, start2, end2);
    int index = low;
    while(start1 <= end1 && start2 <= end2) 
        temp[index++] = arr[start1].second <= arr[start2].second ? arr[start1++] : arr[start2++];
    while(start1 <= end1)
        temp[index++] = arr[start1++];
    while(start2 <= end2)
        temp[index++] = arr[start2++];
    for (int i = low; i<=high; i++)
        arr[i] = temp[i];
}
void checker() {
    for (int i = 0; i<n; i++) {
        if (arr[i] != arrcmp[i]) {
            cout << "Not stable\n";
            return;
        }
        if (i == n-1)
            cout << "Stable\n";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值