Internal Sorting: Straight Two-way Merge Sort: Sorting by Merging

原创 2015年05月31日 14:46:37

Algorithm S

Algorithm S (Straight two-way merge sort). Records R1,… ,RN are sorted
using two memory areas as in Algorithm N.
S1. [Initialize.] Set s <– 0, p <– 1. (For the significance of variables s, i, j, k,
I, and d, see Algorithm N. Here p represents the size of ascending runs to
be merged on the current pass; further variables q and r will keep track of
the number of unmerged items in a run.)
S2. [Prepare for pass.] If s = 0, set i <– 1, j <– N, k <– N, I <– 2N+1; if s = 1,
set i <– N+1, j <– 2N, k <– 0, I <– N+1. Then set d <– 1, q <– p, r <– p.
S3. [Compare Ki:Kj] If Ki > Kj, go to step S8.
S4. [Transmit Ri.] Set k <– k+d, Rk <– Ri.
S5. [End of run?] Set i <– i+1, q <– q-1. If q > 0, go back to step S3.
S6. [Transmit Rj.] Set k <– k+d. Then if k = I, go to step S13; otherwise set
Rk <– Rj.
S7. [End of run?] Set j <– j-1, r <– r-1. If r > 0, go back to step S6;
otherwise go to S12.
S8. [Transmit Rj.] Set k <– k+d, Rk <– Rj.
S9. [End of run?] Set j <– j-1, r <– r-1. If r > 0, go back to step S3.
S10. [Transmit Ri.] Set k <– k+d. Then if k = I, go to step S13; otherwise set
Rk <– Ri .
S11. [End of run?] Set i <– i+1, q <– q-1. If q > 0, go back to step S10.
S12. [Switch sides.] Set q <– p, r <– p, d <– -d, and interchange k <–> I. If
j-i < p, return to step S10; otherwise return to S3.
S13. [Switch areas.] Set p <– p+p. If p < N, set s <– 1-s and return to S2.
Otherwise sorting is complete; if s = 0, set
(R1, … , RN) <– (RN+1, … , R2N).
(The latter copying operation will be done if and only if ceil(lgN) is odd,
regardless of the distribution of the input. Therefore it is possible to predict
the location of the sorted output in advance, and copying will usually be
unnecessary.) |


Flow diagram

Almost the same with Natural two-way merge sort.


Data table

这里写图片描述


Java program

In this program, R1,…,RN were simplified to K1,…,KN.

/**
 * Created with IntelliJ IDEA.
 * User: 1O1O
 * Date: 12/3/13
 * Time: 10:01 PM
 * :)~
 * Straight Two-way Merge Sort:Sorting by Merging:Internal Sorting
 */
public class Main {

    public static void main(String[] args) {
        int N = 16;
        int[] K = new int[33];
        int temp;

        /*Prepare the data*/
        K[1] = 503;
        K[2] = 87;
        K[3] = 512;
        K[4] = 61;
        K[5] = 908;
        K[6] = 170;
        K[7] = 897;
        K[8] = 275;
        K[9] = 653;
        K[10] = 426;
        K[11] = 154;
        K[12] = 509;
        K[13] = 612;
        K[14] = 677;
        K[15] = 765;
        K[16] = 703;

        /*Output unsorted Ks*/
        System.out.println("Unsorted Ks:");
        for(int i=1; i<=N; i++){
            System.out.println(i+":"+K[i]);
        }
        System.out.println();

        /*Kernel of the Algorithm!*/
        int s = 0;
        int p = 1;
        int i = -1;
        int j = -1;
        int k = -1;
        int l = -1;
        int d;
        int q;
        int r;

        do{                            /*S2*/
            if(s == 0){
                i = 1;
                j = N;
                k = N;
                l = 2*N+1;
            }else if(s == 1){
                i = N+1;
                j = 2*N;
                k = 0;
                l = N+1;
            }
            d = 1;
            q = p;
            r = p;
            do{                       /*S3*/
                if(K[i] > K[j]){
                    k += d;           /*S8*/
                    K[k] = K[j];
                    j--;              /*S9*/
                    r--;
                    if(r > 0){
                        continue;
                    }else {
                        do{
                            do{
                                k += d;      /*S10*/
                                if(k == l){
                                    break;
                                }else {
                                    K[k] = K[i];
                                }
                                i++;          /*S11*/
                                q--;
                            }while (q > 0);
                            if(k == l){
                                break;
                            }
                            q = p;              /*S12*/
                            r = p;
                            d = -d;
                            temp = k;
                            k = l;
                            l = temp;
                        }while (j-i < p);
                        if(k == l){
                            break;
                        }
                    }
                }else {
                    k += d;                    /*S4*/
                    K[k] = K[i];
                    i++;                         /*S5*/
                    q--;
                    if(q > 0){
                        continue;
                    }else {
                        do{
                            do{
                                k += d;                /*S6*/
                                if(k == l){
                                    break;
                                }else {
                                    K[k] = K[j];
                                }
                                j--;                 /*S7*/
                                r--;
                            }while (r > 0);
                            if(k == l){
                                break;
                            }
                            q = p;                  /*S12*/
                            r = p;
                            d = -d;
                            temp = k;
                            k = l;
                            l = temp;
                        }while (j-i < p);
                        if(k == l){
                            break;
                        }
                    }
                }
            }while (true);
            p = p + p;                   /*S13*/
            if(p < N){
                s = 1 - s;
            }
        }while (p < N);

        if(s == 0){
            for(int m=1; m<=N; m++){
                K[m] = K[N+m];
            }
        }

        /*Output sorted Ks*/
        System.out.println("Sorted Ks:");
        for(int m=1; m<=N; m++){
            System.out.println(m+":"+K[m]);
        }
    }
}

Outputs

Unsorted Ks:
1:503
2:87
3:512
4:61
5:908
6:170
7:897
8:275
9:653
10:426
11:154
12:509
13:612
14:677
15:765
16:703

Sorted Ks:
1:61
2:87
3:154
4:170
5:275
6:426
7:503
8:509
9:512
10:612
11:653
12:677
13:703
14:765
15:897
16:908

Reference

<< The art of computer programming: Sorting and Searching >> VOLUME 3, DONALD E. KNUTH

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Internal Sorting: Straight selection sort: Sorting by Selection

Straight selection sort:直接选择排序Animation Selection sort animation Selection sort animation. Red is ...
  • IOIO_
  • IOIO_
  • 2015年05月31日 15:48
  • 335

Two-way Merge Sorting 二路归并排序+堆排序--Java实现

一、归并排序算法 基本思想:   归并(Merge)排序法是将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列分为若干个子序列,每个子序列是有序的。然后再把有序子序列合并为整体有序序列...

Internal Sorting: Straight insertion: Sorting by Insertion

Straight insertion sort:直接插入排序Animation Graphical illustration of insertion sort A graphical examp...
  • IOIO_
  • IOIO_
  • 2015年05月31日 16:59
  • 302

Internal Sorting: Radix List Sort: Sorting by Distribution

Algorithm RAlgorithm R (Radix list sort). Records R1,…, RN are each assumed to contain a LINK fiel...
  • IOIO_
  • IOIO_
  • 2015年05月31日 14:13
  • 324

Internal Sorting: Cocktail-shaker sort: Sorting by Exchanging

Cocktail-shaker sortAnimationComplexity Class Sorting algorithm Data structure Array Worst ...
  • IOIO_
  • IOIO_
  • 2015年05月31日 16:26
  • 647

Internal Sorting: Quicksort-2: Sorting by Exchanging

Algorithm QAlgorithm Q (Quicksort). Records R1,...,RNR_1,... ,R_N are rearranged in place; after s...
  • IOIO_
  • IOIO_
  • 2015年05月31日 15:05
  • 306

Internal Sorting: Shellsort: Sorting by Insertion

Shellsort:希尔排序Animation Shellsort with gaps 23, 10, 4, 1 in action. The step-by-step process of re...
  • IOIO_
  • IOIO_
  • 2015年05月31日 16:46
  • 305

Internal Sorting: Quicksort-1:Sorting by Exchanging

Quicksort-1:快速排序-1Animation nimated visualization of the quicksort algorithm. The horizontal lines ...
  • IOIO_
  • IOIO_
  • 2015年05月31日 16:15
  • 232

Internal Sorting: Multiple list insertion: Sorting by Insertion

Data tableJava programIn this program, R1,…,RN were simplified to K1,…,KN./** * Created with Intell...
  • IOIO_
  • IOIO_
  • 2015年05月31日 16:38
  • 265

Internal Sorting: List insertion: Sorting by Insertion

Algorithm LAlgorithm L (List insertion). Records R1,...,RNR_1,..., R_N are assumed to contain keys ...
  • IOIO_
  • IOIO_
  • 2015年05月31日 16:40
  • 409
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Internal Sorting: Straight Two-way Merge Sort: Sorting by Merging
举报原因:
原因补充:

(最多只允许输入30个字)