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 ...
随机推荐
- 如何成功运行一个最简单的servlet
好吧,又是一个简单到不能再简单的问题~~ 由于各种原因,这次就不上图了,直接步骤和代码了. 1.前期准备 jdk.tomcat.EditPlus(eclipse)安装成功并且设置好环境变量. 2.由于 ...
- jquery 农历日历 可自适应
在网上找了许多大牛做的农历日历,但是不是不符合项目中的要求,就是本身就有问题有Bug ,把大牛门的做日历看了n遍 自己又改造了一遍得到了这个:随后日历又要做个自适应的长宽,又在js中改造代码..... ...
- 仿QQ消息气泡提醒
https://github.com/dkmeteor/Bubble-Notification 感谢这位作者,本例子只是封装了一下源码. 这是jar下载地址 下载之后只要引用就好.下面是一个最简单 ...
- Hyperledge 开发环境配置常见问题
一,安装工具 VBOX vagrant vagrant proxy golang 二,下载源代码 hyperledge gotools 三. 1. ==> default: package g ...
- C# 访问MongoDB 通用方法类
using MongoDB.Driver; using System; namespace MongoDBDemo { public class MongoDb { public MongoDb(st ...
- 基于mini2440的Qt移植
花了很长时间,终于把Qt移植到mini2440开发板上了,不能说完全成功,总算是完成一大步,各中过程心酸,废话不多说,下面正式讲解移植过程. 移植环境: win7系统,虚拟机是ubuntu12.04 ...
- C# List结果集排序
public class Student { public string Name { get; set; } public int Age { get; set; } public Student( ...
- 常用MySQL命令整理
备份数据库 #.备份数据库到sql文件 mysqldump --add-drop-database -h localhost -uusername ppassword dbname > dbna ...
- AKKA(一)认知AKKA
Akka 是一个用 Scala 编写的库,用于简化编写容错的.高可伸缩性的 Java 和 Scala 的 Actor 模型应用.它已经成功运用在电信行业.系统几乎不会宕机(高可用性 99.999999 ...
- require.js 入门笔记
网站越来越庞大,JS也是越写越多. 当所有的JS 都集中在 HTML的 head 部分时,网页加载变得很慢,很多的 JS代码也并不是全都适用在当前的页面,造成了代码的冗余度非常高. 而且长长的JS代码 ...