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 原理,通过一趟扫描将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归 ...
随机推荐
- 将 ASP.NET Core 2.1 升级到最新的长期支持版本ASP.NET Core 3.1
目录 前言 Microsoft.AspNetCore.Mvc.ViewFeatures.Internal 消失了 升级到 ASP.NET Core 3.1 项目文件(.csproj) Program. ...
- StackExchange.Redis 之 String 类型示例
String类型很简单,就不做示例演示了,这里只贴出Helper类 /// <summary> /// 判断key是否存在 /// </summary> /// <par ...
- 移动app
什么是移动App开发[重点] 苹果上的软件是如何开发出来的:使用IOS平台的开发工具和开发语言进行设计开发的!苹果上的开发语言:OC.Swift 安卓平台上的软件又是如何开发出来的:使用Java这么语 ...
- Mac 下如何快速重启 Dock 栏?
两种方法. 如果Dock栏出现了问题或是没有反应,请打开Launchpad并按下Command+D键. 这样就可以关闭Dock栏并重启它,效果和经常用到的killall Dock命令相同.
- Golang模块之HTTP
HTTP客户端和服务端 Go语言中内置net/http包提供了HTTP客户端和服务端的实现 HTTP服务端 package main import ( "encoding/json" ...
- Percona Xtrabackup 备份工具
生成备份 $ xtrabackup --backup --target-dir=/data/backups/ 注:--target-dir可以放在my.cnf配置文件中.如果指定的目录不存在,xtra ...
- eclipse maven jdk1.8 还原站点项目红感叹号总是小结
问题背景有三 maven 默认是jdk1.5jdk1.8 目录文件夹不全操作: 在项目上右击-> build path-->config build path-->libraries ...
- Luogu1738 | 洛谷的文件夹 (Trie+STL)
题目描述 kkksc03是个非凡的空想家!在短时间内他设想了大量网页,然后总是交给可怜的lzn去实现. 洛谷的网页端,有很多文件夹,文件夹还套着文件夹. 例如:\(/luogu/application ...
- 如何在CentOS上安装Tensorflow的gpu版本?
系统配置 系统版本: Centos7.6 语言: Python3.5(anaconda3 4.2) 框架: Tensorflow 安装依赖 sudo yum install openjdk-8-jdk ...
- Python3中的支持向量机SVM的使用(有实例)
https://www.cnblogs.com/luyaoblog/p/6775342.html 首先,我们需要安装scikit-learn 一.导入sklearn算法包 在python中导入sci ...