Quick Sort

Let's arrange a deck of cards. Your task is to sort totally n cards. A card consists of a part of a suit (S, H, C or D) and an number. Write a program which sorts such cards based on the following pseudocode:

Partition(A, p, r)
1 x = A[r]
2 i = p-1
3 for j = p to r-1
4 do if A[j] <= x
5 then i = i+1
6 exchange A[i] and A[j]
7 exchange A[i+1] and A[r]
8 return i+1 Quicksort(A, p, r)
1 if p < r
2 then q = Partition(A, p, r)
3 run Quicksort(A, p, q-1)
4 run Quicksort(A, q+1, r)

Here, A is an array which represents a deck of cards and comparison operations are performed based on the numbers.

Your program should also report the stability of the output for the given input (instance). Here, 'stability of the output' means that: cards with the same value appear in the output in the same order as they do in the input (instance).

Input

The first line contains an integer n, the number of cards.

n cards are given in the following lines. Each card is given in a line and represented by a pair of a character and an integer separated by a single space.

Output

In the first line, print the stability ("Stable" or "Not stable") of this output.

In the following lines, print the arranged cards in the same manner of that of the input.

Constraints

  • 1 ≤ n ≤ 100,000
  • 1 ≤ the number of a card ≤ 109
  • There are no identical card in the input

Sample Input 1

6
D 3
H 2
D 1
S 3
D 2
C 1

Sample Output 1

Not stable
D 1
C 1
D 2
H 2
D 3
S 3

Sample Input 2

2
S 1
H 1

Sample Output 2

Stable
S 1
H 1
这道题,还要看排序结果是不是稳定的,已经归并排序是稳定的,所以就拿归并排序的结果与快速排序的结果对比就好了
#include<iostream>
#include<cstring>
#include<stack>
#include<cstdio>
#include<cmath>
using namespace std;
#define MAX 100005
#define INF 2E9
struct Card{
char suit;
int value;
};
struct Card L[MAX/+],R[MAX/+];
void merge(struct Card A[],int n,int left,int mid,int right)
{
int i,j,k;
int n1=mid-left;
int n2=right-mid;
for(int i=;i<n1;i++)
L[i]=A[left+i];
for(int i=;i<n2;i++)
R[i]=A[mid+i];
L[n1].value=R[n2].value=INF;
i=j=;
for(k=left;k<right;k++)
{
if(L[i].value<=R[j].value)
{
A[k]=L[i++];
}
else
A[k]=R[j++];
}
}
void mergeSort(struct Card A[],int n,int left,int right)
{
int mid;
if(left+<right)
{
mid=(left+right)/;
mergeSort(A,n,left,mid);
mergeSort(A,n,mid,right);
merge(A,n,left,mid,right);
}
}
int partition(struct Card A[],int n,int p,int r)
{
int i,j;
struct Card t,x;
x=A[r];
i=p-;
for(j=p;j<r;j++)
{
if(A[j].value<=x.value)
{
i++;
t=A[i];A[i]=A[j];A[j]=t;
}
}
t=A[i+];A[i+]=A[r];A[r]=t;
return i+;
}
void quickSort(struct Card A[],int n,int p,int r)
{
int q;
if(p<r)
{
q=partition(A,n,p,r);
quickSort(A,n,p,q-);
quickSort(A,n,q+,r);
}
} int main()
{
int n,v;
struct Card A[MAX],B[MAX];
char S[];
int stable=;
cin>>n;
for(int i=;i<n;i++)
{
scanf("%s %d",S,&v);
A[i].suit=B[i].suit=S[];
A[i].value=B[i].value=v;
}
mergeSort(A,n,,n);
quickSort(B,n,,n-);
for(int i=;i<n;i++)
if(A[i].suit!=B[i].suit)
stable = ;
if(stable==)
cout<<"Stable"<<"\n";
else
cout<<"Not stable"<<"\n";
for(int i=;i<n;i++)
{
cout<<B[i].suit<<" "<<B[i].value<<"\n";
}
return ;
}

Quick Sort(快速排序)的更多相关文章

  1. PAT甲级——1101 Quick Sort (快速排序)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90613846 1101 Quick Sort (25 分)   ...

  2. Quick Sort -- 快速排序算法

    //参数说明: // int data[] : 待排序的数据数组 // int m : 下限值 // int n : 上限值 void QuickSort ( int data[] , int m , ...

  3. Java中的经典算法之快速排序(Quick Sort)

    Java中的经典算法之快速排序(Quick Sort) 快速排序的思想 基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小, 然后再按此方法对 ...

  4. [算法]——快速排序(Quick Sort)

    顾名思义,快速排序(quick sort)速度十分快,时间复杂度为O(nlogn).虽然从此角度讲,也有很多排序算法如归并排序.堆排序甚至希尔排序等,都能达到如此快速,但是快速排序使用更加广泛,以至于 ...

  5. [算法] 快速排序 Quick Sort

    快速排序(Quick Sort)使用分治法策略. 它的基本思想是:选择一个基准数,通过一趟排序将要排序的数据分割成独立的两部分:其中一部分的所有数据都比另外一部分的所有数据都要小.然后,再按此方法对这 ...

  6. 基础排序算法之快速排序(Quick Sort)

    快速排序(Quick Sort)同样是使用了分治法的思想,相比于其他的排序方法,它所用到的空间更少,因为其可以实现原地排序.同时如果随机选取中心枢(pivot),它也是一个随机算法.最重要的是,快速排 ...

  7. 快速排序(Quick Sort)的C语言实现

    快速排序(Quick Sort)的基本思想是通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对着两部分记录继续进行排序,以达到整个序列有序,具体步骤 ...

  8. 快速排序(Quick Sort)

    快速排序是初学者比较难理解的几个算法之一,这里尽可简单化地讲解,希望能帮到大家. 快速排序基本步骤: 从数列中挑出一个元素,称为"基准"(pivot). 重新排序数列,所有元素比基 ...

  9. 快速排序Quick sort

    快速排序Quick sort 原理,通过一趟扫描将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归 ...

随机推荐

  1. 智和网管平台SugarNMS网络综合监控等级保护安全解决方案

    IT运维是一个很大的范畴,涉及到的部门.架构.技术.产品十分广泛.北京智和信通以等保标准为依据,依托丰富的网络安全行业经验,配套自主研发的智和网管平台SugarNMS,提升用户网络关键基础设施综合管理 ...

  2. RabbitMQ配置死信队列

    死信队列 消息传输过程中难免会产生一些无法及时处理的消息,这些暂时无法处理的消息有时候也是需要被保留下来的,于是这些无法被及时处理的消息就变成了死信. 既然需要保留这些死信,那么就需要一个容器来存储它 ...

  3. golang单元测试简述

      Golang中内置了对单元测试的支持,不需要像Java一样引入第三方Jar才能进行测试,下面将分别介绍Golang所支持的几种测试: 一.测试类型   Golang中单元测试有功能测试.基准测试. ...

  4. 大厂面试必问题!HashMap 怎样解决hash桶碰撞?

    HashMap冲突解决方法比较考验一个开发者解决问题的能力.下文给出HashMap冲突的解决方法以及原理分析,无论是在面试问答或者实际使用中,应该都会有所帮助.在Java编程语言中,最基本的结构就是两 ...

  5. 【已解决2】pyinstaller UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xce in position 110: invalid continuation byte

    python打包exe解码错误问题       最近做了一个小项目,其中把自己写的python打包成exe文件.我用的是pyinstaller. 只需要打包主程序py文件就ok. 在打包过程中,遇到一 ...

  6. (二)LoadRunner目录分析

    学习一个软件的适用,首先应该了解软件目录,对以后深入学习工具有很大的好处. 查看目录文件如下: Analysis Templates——分析模板(默认的模板,可以将自己的模板保存到该目录下) bin— ...

  7. [IOI2018] werewolf 狼人 [kruskal重构树+主席树]

    题意: 当你是人形的时候你只能走 \([L,N-1]\) 的编号的点(即大于等于L的点) 当你是狼形的时候你只能走 \([1,R]\) 的编号的点(即小于等于R的点) 然后问题转化成人形和狼形能到的点 ...

  8. go 函数传递结构体

    我定义了一个结构体,想要在函数中改变结构体的值,记录一下,以防忘记 ep: type Matrix struct{ rowlen int columnlen int list []int } 这是一个 ...

  9. 洛谷题解 P1292 【倒酒】

    原题传送门 题目描述 Winy是一家酒吧的老板,他的酒吧提供两种体积的啤酒,a ml和b ml,分别使用容积为a ml和b ml的酒杯来装载. 酒吧的生意并不好.Winy发现酒鬼们都非常穷.有时,他们 ...

  10. Fhq Treap [FhqTreap 学习笔记]

    众所周知 Fhq Treap 是 fhq 神仙研究出来的平衡树- 具体实现 每个点实现一个 \(\text{rnd}\) 表示 rand 的值 为什么要 rand 呢 是为了保证树高为 \(\log ...