Quick Sort(快速排序)
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(快速排序)的更多相关文章
- PAT甲级——1101 Quick Sort (快速排序)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90613846 1101 Quick Sort (25 分) ...
- Quick Sort -- 快速排序算法
//参数说明: // int data[] : 待排序的数据数组 // int m : 下限值 // int n : 上限值 void QuickSort ( int data[] , int m , ...
- Java中的经典算法之快速排序(Quick Sort)
Java中的经典算法之快速排序(Quick Sort) 快速排序的思想 基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小, 然后再按此方法对 ...
- [算法]——快速排序(Quick Sort)
顾名思义,快速排序(quick sort)速度十分快,时间复杂度为O(nlogn).虽然从此角度讲,也有很多排序算法如归并排序.堆排序甚至希尔排序等,都能达到如此快速,但是快速排序使用更加广泛,以至于 ...
- [算法] 快速排序 Quick Sort
快速排序(Quick Sort)使用分治法策略. 它的基本思想是:选择一个基准数,通过一趟排序将要排序的数据分割成独立的两部分:其中一部分的所有数据都比另外一部分的所有数据都要小.然后,再按此方法对这 ...
- 基础排序算法之快速排序(Quick Sort)
快速排序(Quick Sort)同样是使用了分治法的思想,相比于其他的排序方法,它所用到的空间更少,因为其可以实现原地排序.同时如果随机选取中心枢(pivot),它也是一个随机算法.最重要的是,快速排 ...
- 快速排序(Quick Sort)的C语言实现
快速排序(Quick Sort)的基本思想是通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对着两部分记录继续进行排序,以达到整个序列有序,具体步骤 ...
- 快速排序(Quick Sort)
快速排序是初学者比较难理解的几个算法之一,这里尽可简单化地讲解,希望能帮到大家. 快速排序基本步骤: 从数列中挑出一个元素,称为"基准"(pivot). 重新排序数列,所有元素比基 ...
- 快速排序Quick sort
快速排序Quick sort 原理,通过一趟扫描将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归 ...
随机推荐
- CNN目标检测系列算法发展脉络——学习笔记(一):AlexNet
在咨询了老师的建议后,最近开始着手深入的学习一下目标检测算法,结合这两天所查到的资料和个人的理解,准备大致将CNN目标检测的发展脉络理一理(暂时只讲CNN系列部分,YOLO和SSD,后面会抽空整理). ...
- Go1.14发布了,快来围观新的特性啦
如期而至,Go1.14发布了,和往常一样,该版本保留了Go 1兼容性的承若,这个版本的大部分更新在工具链 .运行时库的性能提升方面,总的来说,还是在已有的基础上不断优化提成,大家期待的泛型还没有到来, ...
- 解决MySql客户端秒退(找不到my.ini)
问题说明(环境:windows7,MySql8.0) 今天安装好MySql后启动MySql服务-->启动服务都失败的就不要往下看了,自行百度解决. 打开客户端秒退,但在cmd中是可以使用数据库的 ...
- ts中的泛型
/** * 泛型:软件工程中,我们不仅要创造定义良好的API,同时也要考虑可重用行,组件不仅能 * 够支持当前的数据类型,同时也能够支持未来数据类型. * 通俗理解:泛型就是解决类.接口.方法的复用性 ...
- C#中的异步编程--探索await与async关键字的奥妙之处,原来理解和使用异步编程可以这么简单
前言 await与async是C#5.0推出的新语法,关于await与async有很多文章讲解.但看完后有没有这样一种感觉,感觉这东西像是不错,但好像就是看不太懂,也不清楚该怎么使用.虽然偶有接触,但 ...
- Ubuntu18--使用vi编辑器方向键以及Backspace乱码问题
解决方法一: 可以进入vi /etc/vim/vimrc.tiny目录对vim配置文件进行修改 修改内容: 修改完成就可以正常使用了.(换行的时候Esc退出编译状态,回车换行,输入第二句话,在按Esc ...
- [Python机器学习]鸢尾花分类 机器学习应用
1.问题简述 假设有一名植物学爱好者对她发现的鸢尾花的品种很感兴趣.她收集了每朵鸢尾花的一些测量数据: 花瓣的长度和宽度以及花萼的长度和宽度,所有测量结果的单位都是厘米. 她还有一些鸢尾花的测量数据, ...
- 数据结构(集合)学习之Collection和Iterator
集合 1.集合与数组 数组(可以存储基本数据类型)是用来存现对象的一种容器,但是数组的长度固定,不适合在对象数量未知的情况下使用. 集合(只能存储对象,对象类型可以不一样)的长度可变,可在多数情况下使 ...
- Python学习笔记--协程asyncio
协程的主要功能是单线程并发运行 假设有3个耗时不一样的任务.看看协程的效果. 先来看没有使用协程情况: #!/usr/bin/python3 # -*- coding:utf-8 -*- import ...
- .NET Core 初次上手Swagger
安装NuGet 程序包=>Swashbuckle.AspNetCore 在 Startup.ConfigureServices 方法里添加注册生成器 //注册Swagger生成器,定义一个和 ...