博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj2299.4thweek.p.A.归并排序
阅读量:4308 次
发布时间:2019-06-06

本文共 1810 字,大约阅读时间需要 6 分钟。

Description:

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence 9 1 0 5 4 ,Ultra-QuickSort produces the output 0 1 4 5 9 .

Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input:

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output:

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

Sample Input

59105431230

Sample Output

60分析: 1.划分问题 2.递归求解. 3.合并 代码及步骤分析:
1 #include
2 #include
3 using namespace std; 4 const int maxn=500005; 5 int a[maxn],b[maxn]; 6 long long k; 7 8 int merge_sort(long long x,long long y) 9 {10 if(x
a[j]) //找到划分后的数组左边的数大于右边的数时19 {20 b[p++]=a[j++];// 将左边数组复制到临时空间21 k+=m+1-i; //注意此时k值的记录!!!22 }23 else24 b[p++]=a[i++]; //将右边数组复制到临时空间25 26 }27 while(i<=m)28 b[p++]=a[i++]; 29 while(j<=y)30 b[p++]=a[j++];31 for(i=0;i
归并排序时间复杂度为O(nlogn),统计改变次数k也并不改变其时间复杂度。
 
 

转载于:https://www.cnblogs.com/x512149882/p/4703723.html

你可能感兴趣的文章
Docker面试题(一)
查看>>
第一轮面试题
查看>>
2020-11-18
查看>>
Docker面试题(二)
查看>>
一、redis面试题及答案
查看>>
消息队列2
查看>>
C++ 线程同步之临界区CRITICAL_SECTION
查看>>
测试—自定义消息处理
查看>>
MFC中关于虚函数的一些问题
查看>>
根据图层名获取图层和图层序号
查看>>
规范性附录 属性值代码
查看>>
提取面狭长角
查看>>
Arcsde表空间自动增长
查看>>
Arcsde报ora-29861: 域索引标记为loading/failed/unusable错误
查看>>
记一次断电恢复ORA-01033错误
查看>>
C#修改JPG图片EXIF信息中的GPS信息
查看>>
从零开始的Docker ELK+Filebeat 6.4.0日志管理
查看>>
How it works(1) winston3源码阅读(A)
查看>>
How it works(2) autocannon源码阅读(A)
查看>>
How it works(3) Tilestrata源码阅读(A)
查看>>