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 list
into 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 ...
随机推荐
- 如何用Visual Studio 2013 (vs2013)编写C语言程序 (转)
http://blog.sina.com.cn/s/blog_964ee6730101jvvi.html
- 转载-------makefile 使用总结
转载自:http://www.cnblogs.com/wang_yb/p/3990952.html 1. Makefile 简介 Makefile 是和 make 命令一起配合使用的. 很多大型项目的 ...
- ListFragment创建及其生命周期
相同的ListFragment 带有一个无参构造 一个有参构造 在该Fragment所依附的Activity视图创建时被实例化一次 方法周期分别为1.无参构造2.onInflate3.onAttach ...
- Linux常见疑难问答
Linux常见疑难问答 (1)按a~z顺序排列启动服务进程. #exportLC_ALL=C #英文环境变量设置,主要用于解决乱码问题 #chkconfig –list | gre ...
- 移动web初体验
1.可视宽度.物理宽度.虚拟宽度,通常自适应都是可视宽度满屏.即设置meta <meta name="viewport" content="width=device ...
- Spring集成Memcached三种方式(一)
转载:http://blog.csdn.net/u013725455/article/details/52102170 Memcached Client目前有3种: Memcached Client ...
- Memcache的使用和协议分析详解
Memcache的使用和协议分析详解 作者:heiyeluren博客:http://blog.csdn.NET/heiyeshuwu时间:2006-11-12关键字:PHP Memcache Linu ...
- 跨域的get和post的区别
GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二.最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数.当你在面试中被 ...
- 实数---Currency讲解
Currency 实际上是 Int64 的变体,Int64/10000 就是实际的值
- Android之全局的dialog的显示
今天做项目有个需求就是有一个页面需要弹出一个dialog,但是这个dialog不可以影响,这个页面的跳转.这个页面可能跳转也可能不跳转,跳转后,这个dialog,还是显示的, 然而我们平时写的dial ...