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 sortedN 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的更多相关文章

  1. java.util.List

    /* * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETA ...

  2. Timsort 算法

    转载自:http://blog.csdn.net/yangzhongblog/article/details/8184707 Timsort是结合了合并排序(merge sort)和插入排序(inse ...

  3. Java基础-集合框架-ArrayList源码分析

    一.JDK中ArrayList是如何实现的 1.先看下ArrayList从上而下的层次图: 说明: 从图中可以看出,ArrayList只是最下层的实现类,集合的规则和扩展都是AbstractList. ...

  4. TimSort Java源码个人解读

    /*JDK 1.8 */ package java.util; /** * A stable, adaptive, iterative mergesort that requires far fewe ...

  5. Collection接口的子接口——List接口

    https://docs.oracle.com/javase/8/docs/api/java/util/List.html public interface List<E> extends ...

  6. 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 ...

  7. Java的集合(一)

    转载:https://blog.csdn.net/hacker_zhidian/article/details/80590428 Java集合概况就三个:List.set和map list(Array ...

  8. Iterative (non-recursive) Merge Sort

    An iterative way of writing merge sort: #include <iostream> using namespace std; void merge(in ...

  9. 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 ...

随机推荐

  1. java @Autowired与@Resource的区别

    @Autowired与@Resource的区别     1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认 ...

  2. beagleBone black 中QT的移植

    收到板子后默认是Debian系统.gcc -v 后发现其编译链是:arm-linux-gnueabihf http://pan.baidu.com/s/1pJrAvsn 有相关工具下载,里面有这个 解 ...

  3. ORA-04021:timeout occurred while waiting to lock object

    编译某存储过程 ORA-04021 timeout occurred while waiting to lock object stringstringstringstringstring Cause ...

  4. 创建DAO模式的步骤

    1.建立数据库epet 2.创建实体类,和相对应的数据库是对应的 3.创建Dao的基类接口类BaseDao 4.创建Dao的实现类BaseDaoImpl 5.创建具体表的Dao类 6.创建具体表的Da ...

  5. CSS常用样式及示例

    CSS常用样式及示例 一.简介      层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集) ...

  6. Tcp/IP

     在数据传输完毕之后,通信双方都可以发出释放连接的请求.释放连接的过程为如上图所示:      1)数据传输结束后,主机A的应用进程先向其TCP发出释放连接请求,不在发送数据.TCP通知对方要释放从A ...

  7. ES6学习笔记(2)

    变量的解构赋值 ES6允许按照一定模式,从数组和对象中提取值,对变量进行赋值,被称为解构(Destructuring); 数组的解构赋值 let [a, b, c] = [1, 2, 3]; cons ...

  8. 用Python读写Excel文件(转)

    原文:google.com/ncr 虽然天天跟数据打交道,也频繁地使用Excel进行一些简单的数据处理和展示,但长期以来总是小心地避免用Python直接读写Excel文件.通常我都是把数据保存为以TA ...

  9. 将Linux命令的结果作为下一个命令的参数

    查询所有的pid并杀死. jps -l | grep bdcsc2-native-demo | awk '{print $1}' | xargs kill -9 KISS:keep it short ...

  10. Nodejs学习(四)- express目录的分析

    好久不来了,最近挺忙,就写一写下目录的情况吧. 我就说主要的目录,也就是我们经常用到的 public  用于存放一些js,css. routes 路由目录,如果你学过MVC应该不默生. views   ...