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. ANSYS经典APDL编程

    在使用ANSYS的过程中的一些经验总结: Ansys Workbench 有限元分析虽然进入UI阶段,但是语言命令仍然是其基础核心. 1.ANSYS中的一些关键概念的理解; 参数化程序设计语言(APD ...

  2. java中abstract详解

    Abstract(抽象)可以修饰类.方法 如果将一个类设置为abstract,则此类必须被继承使用.此类不可生成对象,必须被继承使用. Abstract可以将子类的共性最大限度的抽取出来,放在父类中, ...

  3. .net操作数据库,史上最牛逼的方法,你见过这种方法吗

    免费分享给大家.下载地址在最下面. C# code   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...

  4. 脚本工具: 查看当前系统被写入的FD

    #!/bin/bash touch /tmp/sn2 /tmp/sn4 /tmp/sn6 /tmp/sn3 redir=/dev/null which lsof >&/dev/null ...

  5. cnblogs,我回来了

    之前是在Github上搭了个博客,原因只有一个:可以弄个比较个性的域名,逼格高. 不过用起来倒是麻烦,一是经常纠结自己的主页是不是不够逼格?二就是身在墙内,访问速度不理想. 所以,还是安心的在这里,写 ...

  6. JAVA初学(1):值类型和引用类型的区别

    JAVA值类型和引用类型的区别(转)                                                          [定义] 引用类型表示你操作的数据是同一个,也就 ...

  7. 自己实现多线程的socket,socketserver源码剖析

    1,IO多路复用 三种多路复用的机制:select.poll.epoll 用的多的两个:select和epoll 简单的说就是:1,select和poll所有平台都支持,epoll只有linux支持2 ...

  8. PHP审计小记

    /* 在漏洞时代看了一篇文章,说到一个通用函数如何绕过.那么我就来看看这套程序 */ foreach($_REQUEST as $_k=>$_v) { if( strlen($_k)>0 ...

  9. react使用过程记录

    1, webpack使用,如果发现浏览器打不开,检查是不是 端口冲突 2, webpack报错,如下,加粗是不是 lodash加载的问题 >> A special character wa ...

  10. 自动化测试第一季-selenium + python(环境搭建与基础代码解释)

    # coding = utf-8              %%%%%%%%%%%%%%%%防止乱码(可加可不加) from selenium import webdriver             ...