Merge Sort
  • Best Case, O(nlogn)
  • Worst Case, Ω(nlogn)
  • Memory Complexity, O(n)
  • import java.util.Random;
    import java.util.Arrays;
    
    public class Merge
    {
        // Merges two subarrays of arr[].
        // First subarray is arr[l..m]
        // Second subarray is arr[m+1..r]
        public static void merge(int arr[], int l, int m, int r)
        {
            // Find sizes of two subarrays to be merged
            int n1 = m - l + 1;
            int n2 = r - m;
     
            /* Create temp arrays */
            int L[] = new int [n1];
            int R[] = new int [n2];
     
            /*Copy data to temp arrays*/
            for (int i=0; i<n1; ++i)
                L[i] = arr[l + i];
            for (int j=0; j<n2; ++j)
                R[j] = arr[m + 1+ j];
     
     
            /* Merge the temp arrays */
     
            // Initial indexes of first and second subarrays
            int i = 0, j = 0;
     
            // Initial index of merged subarry array
            int k = l;
            while (i < n1 && j < n2)
            {
                if (L[i] <= R[j])
                {
                    arr[k] = L[i];
                    i++;
                }
                else
                {
                    arr[k] = R[j];
                    j++;
                }
                k++;
            }
     
            /* Copy remaining elements of L[] if any */
            while (i < n1)
            {
                arr[k] = L[i];
                i++;
                k++;
            }
     
            /* Copy remaining elements of R[] if any */
            while (j < n2)
            {
                arr[k] = R[j];
                j++;
                k++;
            }
        }
    
        // merge()
        public static void sort(int arr[], int l, int r)
        {
            if (l < r)
            {
                // Find the middle point
                int m = (l+r)/2;
    
                // Sort first and second halves
                sort(arr, l, m);
                sort(arr , m+1, r);
    
                // Merge the sorted halves
                merge(arr, l, m, r);
            }
        }
    
        //generate an array with random elements
        public static int [] getArray(int size)
        {
    	    int [] array = new int[size];
    	    Random r = new Random();
    
    	    for(int i = 0; i < size; i++)
    		    array[i] = r.nextInt(100);
    
    	    return array;
        }
    
        public static void main(String args[])
        {
    	    long start, end;
    	    int [] array;
    	    int [] array2;
    	    int size = 100000;
    
    	    //create two same arrays
    	    array = getArray(size);
    	    array2 = Arrays.copyOf(array, array.length);
    
    	    //sort a huge array with bubble sort
    	    start = System.currentTimeMillis();
    	    sort(array, 0, array.length-1);
    	    end = System.currentTimeMillis();
    	    System.out.printf("Bubble sort time: %10f%n", (end-start)/1000000.0);
    
    	    //sort the array with build-in sort function in Arrays class
    	    start = System.currentTimeMillis();
    	    Arrays.sort(array2);
    	    end = System.currentTimeMillis();
    	    System.out.printf("Build-in sort time: %10f%n", (end-start)/1000000.0);
    
    	    //validate merge sort
    	    if(Arrays.equals(array, array2))
    		    System.out.println("Merge sort is validated ...");
    	    else
    		    System.out.println("Merge sort does not work correctly ...");
        }
    }