### The naiverecursive (top-down) merge sort algorithm

• Dictactical note

• Note:

• This webpage will present an easier to understand but inefficient merge sort algorithm

 I will call this version naive....

• This naive version will create many arrays for each recursion

 Other than this short-coming, the naive version of merge sort is identical to the text-book version.

• In the next webpage, I will show you the text-book version of the merge sort algorithm.

• From using BubbleSort to using a copy of itself

• Consider the Simplified merge sort algorithm:

 ``` public static void sort(double[] a) { double[] left, right; if ( a.length == 1 ) { // No need to sort ! return; } /* ================================================================= Split a[0 ..... middle-1 middle .... a.length-1] / \ left[0 .. middle-1] right[0 ... a.length-1-middle] ================================================================= */ int middle = a.length/2; left = new double[middle]; // Create array to hold left half for ( int i = 0; i < middle; i++ ) left[i] = a[i]; right = new double[a.length-middle]; // Create array to hold right half for ( int i = 0; i < a.length-middle; i++ ) right[i] = a[i+middle]; /* ====================================== Sort both halves of the arrays Let's use BubbleSort.... ====================================== */ BubbleSort.sort( left ); BubbleSort.sort( right ); /* ====================================== Merge both sorted arrays back ====================================== */ merge( a, left, right ); // We have discussed the Merge alg. already... } ```

Idea:

 The merge sort algorithm accomplishes the same thing as the Bubble Sort algorithm. Let's use the merge sort algorithm instead !!!

• Result:

 ``` public static void sort(double[] a) { double[] left, right; if ( a.length == 1 ) { // No need to sort ! return; } /* ================================================================= Split a[0 ..... middle-1 middle .... a.length-1] / \ left[0 .. middle-1] right[0 ... a.length-1-middle] ================================================================= */ int middle = a.length/2; left = new double[middle]; // Create array to hold left half for ( int i = 0; i < middle; i++ ) left[i] = a[i]; right = new double[a.length-middle]; // Create array to hold right half for ( int i = 0; i < a.length-middle; i++ ) right[i] = a[i+middle]; /* ====================================== Sort both halves of the arrays Let's use MergeSort !!! ====================================== */ sort( left ); // Recursion sort( right ); // Recursion /* ====================================== Merge both sorted arrays back ====================================== */ merge( a, left, right ); // We have discussed the Merge alg. already... } ```

Sample output:

 ```Before sort: [6.4, 3.5, 7.5, 2.5, 8.9, 4.2, 9.2, 1.1] After sort: [1.1, 2.5, 3.5, 4.2, 6.4, 7.5, 8.9, 9.2] ```

How the algorithm works:

• The algorithm will keep splitting the input array until an array of one element is found:

An array of one element is sorted and sort() will return the sorted array back

• Then the second half of that array will be sorted:

It is also and array of one element...

• Now both halves have been sorted and the algorithm will perform a merge operation:

• The merged result is then returned:

• Then the second half [3.5   7.5] of that array will be sorted:

The array is split and each halft is sorted (--- just like the first half [10.2   6.4])

I will skip the picture showing the second half being return and go to the merge operation

• Now both halves have been sorted and the algorithm will perform a merge operation:

• The merged result is then returned:

• Now both halves have been sorted and the algorithm will perform a merge operation:

• The merged result is then returned:

• Only then, the algorithm will work on the second half of the original array

The process is the same one as you have seen that sorts the first half of the original array

So I will not make the picture.... but only give you the final result:

• The 2 sorted arrays are then merged to produce the final result:

• Example Program: (Demo above code)

How to run the program:

 Right click on link(s) and save in a scratch directory To compile:   javac testProg0.java To run:          java testProg0

• Nomenclature

• The recursive merge sort algorithm is also known as:

• The top-down merge sort algorithm

• This is because the sorting actions performed is as follows:

 ``` Sort: [6.4, 3.5, 7.5, 2.5, 8.9, 4.2, 9.2, 1.1] Split:[6.4, 3.5, 7.5, 2.5] | [8.9, 4.2, 9.2, 1.1] Another Merge Sort starts to sort the Left[ ] array: Sort: [6.4, 3.5, 7.5, 2.5] Split:[6.4, 3.5] | [7.5, 2.5] Another Merge Sort starts to sort the Left[ ] array Sort: [6.4, 3.5] And so on... ```

• Top = the whole array

• Bottom = smaller pieces of the array

• Because the algorithm starts working on the whole array first, (and this results in working on the smaller array next), the algorithm is called top-down

• I have rigged the recursive merge sort algorithm to print out the input arrays at the start of each recursion

Sample output:

 ```Input: [6.4, 3.5, 7.5, 2.5, 8.9, 4.2, 9.2, 1.1] Sort: [6.4, 3.5, 7.5, 2.5, 8.9, 4.2, 9.2, 1.1] Split:[6.4, 3.5, 7.5, 2.5] | [8.9, 4.2, 9.2, 1.1] Sort: [6.4, 3.5, 7.5, 2.5] Split:[6.4, 3.5] | [7.5, 2.5] Sort: [6.4, 3.5] Split:[6.4] | [3.5] Sort: [6.4] Result: [6.4] Sorted Left half: [6.4] Sort: [3.5] Result: [3.5] Sorted Right half: [3.5] Merged Result: [3.5, 6.4] Sorted Left half: [3.5, 6.4] Sort: [7.5, 2.5] Split:[7.5] | [2.5] Sort: [7.5] Result: [7.5] Sorted Left half: [7.5] Sort: [2.5] Result: [2.5] Sorted Right half: [2.5] Merged Result: [2.5, 7.5] Sorted Right half: [2.5, 7.5] Merged Result: [2.5, 3.5, 6.4, 7.5] Sorted Left half: [2.5, 3.5, 6.4, 7.5] Sort: [8.9, 4.2, 9.2, 1.1] Split:[8.9, 4.2] | [9.2, 1.1] Sort: [8.9, 4.2] Split:[8.9] | [4.2] Sort: [8.9] Result: [8.9] Sorted Left half: [8.9] Sort: [4.2] Result: [4.2] Sorted Right half: [4.2] Merged Result: [4.2, 8.9] Sorted Left half: [4.2, 8.9] Sort: [9.2, 1.1] Split:[9.2] | [1.1] Sort: [9.2] Result: [9.2] Sorted Left half: [9.2] Sort: [1.1] Result: [1.1] Sorted Right half: [1.1] Merged Result: [1.1, 9.2] Sorted Right half: [1.1, 9.2] Merged Result: [1.1, 4.2, 8.9, 9.2] Sorted Right half: [1.1, 4.2, 8.9, 9.2] Merged Result: [1.1, 2.5, 3.5, 4.2, 6.4, 7.5, 8.9, 9.2] Output: [1.1, 2.5, 3.5, 4.2, 6.4, 7.5, 8.9, 9.2] ```

You can run the rigged version to see how the recursion will split the array and use recursion to sort the smaller array

• Example Program: (Demo above code)

How to run the program:

 Right click on link(s) and save in a scratch directory To compile:   javac testProg1.java To run:          java testProg1