这个题写快排的时候必须按照题目中的思路走,要不然会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";
}
}