CS 415, Section 001 Sonoma State University Fall, 2022
 
Algorithm Analysis
Instructor: Henry M. Walker

Lecturer, Sonoma State University
Professor Emeritus of Computer Science and Mathematics, Grinnell College

Although much of this course is well developed, some details can be expected to evolve as the semester progresses.
Any changes in course details will be announced promptly during class.

Reading:  Merge Sort

The following sorting algorithm is called a merge sort, because it is based on a simple procedure that combines or merges two short lists of ordered data into one large, ordered list. With this procedure, the merge sort proceeds from small initial array segments to large ones:

To clarify, this process is illustrated in the following diagram:

merge sort example

As this process indicates, for each step, the merge sort considers an array to be a collection of small, ordered segments, and two adjacent segments can be merged to yield a larger, ordered array segment.

In practice, this merging process is subject to several details:

The following method puts these elements together, assuming the merge method has already been implemented.

      /** 
     * sort the array a, using a merge sort
     */
    public static  void  mergeSort (int initArr [ ]) {
	int resArr [ ] = new int [initArr.length];
	int a0[ ] = initArr;
	int a1[ ] = resArr;
	boolean needCopyBack = false;  
	
	int mergeSize = 1;
	int start1;
	while (mergeSize < initArr.length)
	    {
		int end2;
		for (start1 = 0; start1 < initArr.length; start1 = end2) {
		    int start2 = start1 + mergeSize;
		    end2 = start2 + mergeSize;
		    merge (a0, a1, start1, start2, end2);
		}

		// swap a0, a1 pointers, so a0 is starting array for next merge
		int temp [ ] = a0;
		a0 = a1;
		a1 = temp;
		mergeSize *= 2;

		// keep track of which array holds initArr object
		needCopyBack = !needCopyBack;

                //print intermediate result, if desired
                for (int i = 0; i < a0.length; i++)
		    System.out.printf ("%5d", a0[i]);
                System.out.print ("\n\n");

	    }

	//copy result into initArr, as needed
	if (needCopyBack)
	    {
		for (int i = 0; i < initArr.length; i++)
		    initArr [i] = a0 [i];
	    }
    }

With this main step completed, the remaining work involves merging two sorted array segments. The outline for this merge follows:

Consistent with the merge sort code above, an appropriate header for this merge method might be:

    /** 
     * merge aInit[start1]..aInit[start1+mergeSize] with 
     *       aInit[start2]..Init[end2]
     *       with the result placed in aRes
     * Note:  it may be that start2 >= aInit.length, in which case, only the
     *        valide part of aInit[start1] is copied
     */
    private static void merge (int aInit [ ], int aRes [ ],
			       int start1, int start2, int end2) 
created 24 April 2001
revised 11 March 2005
revised 9-13 March 2012
revised 3 November 2018
created December, 2021
revised December-January 2021
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.