
public static void sort(double[] a) { double[] left, right; if ( a.length == 1 ) { // No need to sort ! return; } /* ================================================================= Split a[0 ..... middle1 middle .... a.length1] / \ left[0 .. middle1] right[0 ... a.length1middle] ================================================================= */ 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.lengthmiddle]; // Create array to hold right half for ( int i = 0; i < a.lengthmiddle; 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:

public static void sort(double[] a) { double[] left, right; if ( a.length == 1 ) { // No need to sort ! return; } /* ================================================================= Split a[0 ..... middle1 middle .... a.length1] / \ left[0 .. middle1] right[0 ... a.length1middle] ================================================================= */ 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.lengthmiddle]; // Create array to hold right half for ( int i = 0; i < a.lengthmiddle; 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:

How to run the program:


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
How to run the program:
