PTA Iterative Mergesort
How would you implement mergesort without using recursion?
The idea of iterative mergesort is to start from N sorted sublists of length 1, and each time to merge a pair of adjacent sublists until one sorted list is obtained. You are supposed to implement the key function of merging.
Format of functions:
void merge_pass( ElementType list[], ElementType sorted[], int N, int length );
The function merge_pass performs one pass of the merge sort that merges adjacent pairs of sublists from listinto sorted. N is the number of elements in the list and length is the length of the sublists.
Sample program of judge:
#include <stdio.h>
#define ElementType int
#define MAXN 100
void merge_pass( ElementType list[], ElementType sorted[], int N, int length );
void output( ElementType list[], int N )
{
int i;
for (i=0; i<N; i++) printf("%d ", list[i]);
printf("\n");
}
void merge_sort( ElementType list[], int N )
{
ElementType extra[MAXN]; /* the extra space required */
int length = 1; /* current length of sublist being merged */
while( length < N ) {
merge_pass( list, extra, N, length ); /* merge list into extra */
output( extra, N );
length *= 2;
merge_pass( extra, list, N, length ); /* merge extra back to list */
output( list, N );
length *= 2;
}
}
int main()
{
int N, i;
ElementType A[MAXN];
scanf("%d", &N);
for (i=0; i<N; i++) scanf("%d", &A[i]);
merge_sort(A, N);
output(A, N);
return 0;
}
/* Your function will be put here */
Sample Input:
10
8 7 9 2 3 5 1 6 4 0
Sample Output:
7 8 2 9 3 5 1 6 0 4
2 7 8 9 1 3 5 6 0 4
1 2 3 5 6 7 8 9 0 4
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
解答
这题有一个要注意的地方,就是每次归并最后剩下的数列长记为x,如果x <= length的话,就不需要归并了,直接赋值,如果x > length的话,就是一个长为length的子列和一个长为x - length的子列归并。
void merge_pass( ElementType list[], ElementType sorted[], int N, int length ){
;
; i < N; i += length * ){
< N){
j = i;
k = j + length;
* length){
if(list[j] > list[k]){
sorted[index++] = list[k++];
}
else{
sorted[index++] = list[j++];
}
}
while(j < i + length){
sorted[index++] = list[j++];
}
* length){
sorted[index++] = list[k++];
}
}
else if(N - i > length){
j = i;
k = j + length;
while(j < i + length&&k < N){
if(list[j] > list[k]){
sorted[index++] = list[k++];
}
else{
sorted[index++] = list[j++];
}
}
while(j < i + length){
sorted[index++] = list[j++];
}
while(k < N){
sorted[index++] = list[k++];
}
}
else{
j = i;
while(j < N){
sorted[index++] = list[j++];
}
}
}
}
PTA Iterative Mergesort的更多相关文章
- java.util.List
/* * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETA ...
- Timsort 算法
转载自:http://blog.csdn.net/yangzhongblog/article/details/8184707 Timsort是结合了合并排序(merge sort)和插入排序(inse ...
- Java基础-集合框架-ArrayList源码分析
一.JDK中ArrayList是如何实现的 1.先看下ArrayList从上而下的层次图: 说明: 从图中可以看出,ArrayList只是最下层的实现类,集合的规则和扩展都是AbstractList. ...
- TimSort Java源码个人解读
/*JDK 1.8 */ package java.util; /** * A stable, adaptive, iterative mergesort that requires far fewe ...
- Collection接口的子接口——List接口
https://docs.oracle.com/javase/8/docs/api/java/util/List.html public interface List<E> extends ...
- Java-Class-I:java.util.List
ylbtech-Java-Class-I:java.util.List 1.返回顶部 1.1.import java.util.ArrayList;import java.util.List; 1.2 ...
- Java的集合(一)
转载:https://blog.csdn.net/hacker_zhidian/article/details/80590428 Java集合概况就三个:List.set和map list(Array ...
- Iterative (non-recursive) Merge Sort
An iterative way of writing merge sort: #include <iostream> using namespace std; void merge(in ...
- PTA 09-排序2 Insert or Merge (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/675 5-13 Insert or Merge (25分) According to ...
随机推荐
- Oracle数据库BLOB字段的存取
述] Oracle的Blob字段比较特殊,他比long字段的性能要好很多,可以用来保存例如图片之类的二进制数据. 写入Blob字段和写入其它类型字段的方式非常不同,因为Blob自身有一个cur ...
- Android安装apk时报错:INSTALL_FAILED_NO_MATCHING_ABIS
问题背景 OS:无关 AS:无关 Genymotion:2.5.2 Virtual Device:Google Nexus 5 - 5.1.0 - API 22 原因分析 CPU架构不符 解决方案 对 ...
- FileUpload 上传文件,并实现c#使用Renci.SshNet.dll实现SFTP文件传输
fileupload上传文件和jquery的uplodify控件使用方法类似,对服务器控件不是很熟悉,记录一下. 主要是记录新接触的sftp文件上传.服务器环境下使用freesshd搭建好环境后,wi ...
- WIN7-修改域名
前言:访问本地项目有如下的几种方式,其中通过修改域名配置文件的方式使用的较少,不过在某些项目中也会使用的到(比如:我曾经参与的一个项目使用到SpringCloud框架,需要将每个微服务注册到,服务控制 ...
- JavaScript简介及示例
JavaScript简介及使用 一.简介 JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛 ...
- jQuery validate兼容IE8写法
最近做项目的时候遇到一个validate插件在IE8下面点击submit按钮没有执行检查的BUG 在chrome和FF,还有IE9以上都可以.百度了好多文章都没有找到解决方法,后面自己测试找到了问题. ...
- Openstack Day1简介及虚拟环境搭建
本文章仅作为作者本人存档记忆!恕不详细展开内容! openstack kilo版本重要组件(module)简介 ======================================= Ke ...
- PHP正则表达式模式修饰符 /i, /is, /s, /isU等
模式修饰符 下面列出了当前可用的 PCRE 修饰符.括号中提到的名字是 PCRE 内部这些修饰符的名称. 模式修饰符中的空格,换行符会被忽略,其他字符会导致错误. i (PCRE_CASELESS) ...
- ASP.NET MVC4框架揭秘 源代码下载
http://files.cnblogs.com/artech/asp.net.mvc.4.samples.rar
- sql 时间(datetime)计算
SELECT *FROM sc_sowu_orderreturnWHERE STATUS = '0'AND submit_time < DATE_ADD(now(), INTERVAL - 4 ...