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 原理,通过一趟扫描将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归 ...
随机推荐
- Django 系列
Django基础 Django框架简介 Django 静态文件 Django request对象与ORM简介 Django路由系统 Django之视图层 Django之模板层 Django的setti ...
- C# bubble sort,selection sort,insertion sort
static void Main(string[] args) { InsertionSortDemo(); Console.ReadLine(); } static void InsertionSo ...
- iOS异常采用处理方式
iOS开发过程中我们经常会遇到异常问题 对异常的处理一般采用打印或者直接抛出.这样可以很方便我们调试过程有所参考,而且方便我们查看异常产生的位置信息 NSError(错误信息) 采用NSError的情 ...
- [20200223]关于latch and mutext的优化.txt
[20200223]关于latch and mutext的优化.txt --//前一段时间一直在测试使用DBMS_SHARED_POOL.MARKHOT标识热对象以及sql语句的优化.--//有别人问 ...
- Linux btrfs文件系统
btrfs,它名字挺多:B-tree fs;Butter fs;Better fs 开源协议是GPL,2007年由Oracle研发 核心特性: 多物理卷支持,btrfs可由多个物理卷组成:支持RAID ...
- .netcore 3.1高性能微服务架构:封装调用外部服务的接口方法--HttpClient客户端思路分析
众所周知,微服务架构是由一众微服务组成,项目中调用其他微服务接口更是常见的操作.为了便于调用外部接口,我们的常用思路一般都是封装一个外部接口的客户端,使用时候直接调用相应的方法.webservice或 ...
- Vue之计算属性Computed和属性监听Watch,Computed和Watch的区别
一. 计算属性(computed) 1.计算属性是为了模板中的表达式简洁,易维护,符合用于简单运算的设计初衷. 例如: <div id="app"> {{ myname ...
- (未完成)【Android】MVP模式初见(一)
最近在阅读郭霖大神的公众号时,分类中架构引起了我的注意. 虽然是个人开发(水平很菜的那种),但最终都要向企业正式项目开发靠近.因此接下来一段时间,主要学习一下MVP架构.Retrofit以及RxJav ...
- css基础-定位+网页布局案例
position:static 忽略top/bottom/left/right或者z-index position:relative 设置相对定位的元素不会脱离文档流 position:fixed 不 ...
- 图片上传的进度条-jquery
<div style="padding: 10px;"> <div class="progress-bar" style="disp ...