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 原理,通过一趟扫描将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归 ...
随机推荐
- React之拆分组件与组件之间的传值
父子组件传值: 父组件向子组件传值通过向子组件TodoItem进行属性绑定(content={item}.index={index}),代码如下 getTodoItem () { return thi ...
- Class Literal(Java)
前言 上一节我们讨论过通过关键字synchronized实现线程同步,同时最主要了解到在Java中className.class所代表的具体含义,在博客写完后,感觉还是有点迷糊,然后再次深入了解后,原 ...
- jQuery瀑布流插件masonry
项目要做荣誉证书的排版,宽度是统一的,但是高度不一致 采用瀑布流的效果来实现 默认先实现前15张,点击按钮再加载全部剩下的数据 效果图 首先是html部分,写好样式 <!-- 荣誉资质 --&g ...
- Dynamics 365 marketing中添加自定义渠道磁贴
Dynamics 365 marketing中默认的渠道只有Marketing Email和Marketing Activity,想要添加其他渠道必须自定义磁贴,自定义磁贴的步骤如下: 1.创建实体 ...
- Android Studio 学习笔记(五):WebView 简单说明
Android中一个用于网页显示的控件,实际上,也可以看做一个功能最小化的浏览器,看起来类似于在微信中打开网页链接的页面.WebView主要用于在app应用中方便地访问远程网页或本地html资源.同时 ...
- 腾讯2020校园招聘-后台&综合-第一次笔试 题解
对数据结构和算法感兴趣的可以关注一下https://github.com/MCQ1999/Datastructure_Algorithm_Solutions,分享算法题的解题思路和代码~ 1.压缩算法 ...
- vue及vant框架,多语言配置
1.安装 vue-i18n,( cnpm install vue-i18n --save ) 2.在入口,main.js 中引入 (import Vuei18n from "vue-i18n ...
- vim 进化 编码问题
" 解决菜单乱码 source $VIMRUNTIME/delmenu.vim source $VIMRUNTIME/menu.vim " 防止文件显示乱码 set fileenc ...
- promise链式调用
var that = this;that.hello().then(res => { return that.world(res);}).then(res => { console.log ...
- Verilog-同步FIFO
参考博客:https://blog.csdn.net/hengzo/article/details/49683707 1.基本框图 1)双端口RAM加两个读写指针 2)写数据.写使能.写满:读数据.读 ...