Module-2 (3 & 4 Lab Programs) Merge Sort
Module-2 (3 & 4 Lab Programs) Merge Sort
3) Sort a given set of n integer elements using Merge Sort method and Compute its time complexity. Run
the program for varied values of n > 5000, and record the time taken to sort. Demonstrate using Java how
the divide and conquer method works along with its time complexity analysis: worst case, average case
and best case.
import java.util.Random;
import java.util.Scanner;
start=System.nanoTime();
mergesort(a,0,n-1);
end=System.nanoTime();
static void merge(int a[], int low, int mid, int high)
{
int i, j, h, k, b[]= new int[100000];
h=low;
i=low;
j=mid+1;
}
}
The sorted
elements are:3
4
5
6
7
11
20
…..
The time taken to sort is 1173776857 ns
******** ********************** *******
OUTPUT 2:
********** MERGE SORT PROGRAM*******
Enter the number of elements to be sorted
6000
Array elements to be sorted are :
2853
3407
8435
8882
3709
…
The sorted elements are :
0
0
1
1
1
….
The time taken to sort is 1256530873 ns
******** ********************** *******
Module-2 Write a Java program with following: Merge sort involves recursively splitting the array into 2
parts, sorting and finally merging them. A variant of merge sort is called 3-way merge sort where instead
of splitting the array into 2 parts we split it into 3 parts. Merge sort recursively breaks down the arrays to
subarrays of size half. Similarly, 3-way Merge sort breaks down the arrays to subarrays of size one third.
Input : 45, -2, -45, 78, 30, -42, 10,19,73,93
Output : -45, -42, -2, 10, 19, 30, 45, 73, 78, 93
Input : 23, -19
Output : -19, 23
import java.util.*;
// sort function
mergeSort3WayRec(fArray, 0, gArray.length, gArray);
/* Performing the merge sort algorithm on the given array of values in the rangeof indices (low, high). */
public static void mergeSort3WayRec(Integer[] gArray,int low, int high, Integer[] destArray)
{
// If array size is 1 then do nothing
if (high - low < 2)
return;
/* Merge the sorted ranges (low, mid1), (mid1, mid2) and (mid2, high) */
public static void merge(Integer[] gArray, int low, int mid1, int mid2, int high,Integer[] destArray)
{
int i = low, j = mid1, k = mid2, l = low;
while ((i < mid1) && (j < mid2) && (k < high)) // choose smaller of the smallest in the three ranges
{
if (gArray[i].compareTo(gArray[j]) < 0)
{
if (gArray[i].compareTo(gArray[k]) < 0)
destArray[l++] = gArray[i++];
else
destArray[l++] = gArray[k++];
}
else
{
if (gArray[j].compareTo(gArray[k]) < 0)
destArray[l++] = gArray[j++];
else
destArray[l++] = gArray[k++];
}
}
while ((i < mid1) && (j < mid2)) // case where first and second ranges have remaining values
{
if (gArray[i].compareTo(gArray[j]) < 0)
destArray[l++] = gArray[i++];
else
destArray[l++] = gArray[j++];
}
while ((j < mid2) && (k < high)) // case where second and third ranges have remaining values
{
if (gArray[j].compareTo(gArray[k]) < 0)
destArray[l++] = gArray[j++];
else
destArray[l++] = gArray[k++];
}
while ((i < mid1) && (k < high)) // case where first and third ranges have remaining values
{
if (gArray[i].compareTo(gArray[k]) < 0)
destArray[l++] = gArray[i++];
else
destArray[l++] = gArray[k++];
}
while (i < mid1) // copy remaining values from the first range
destArray[l++] = gArray[i++];
while (j < mid2) // copy remaining values from the second range
destArray[l++] = gArray[j++];
while (k < high) // copy remaining values from the third range
destArray[l++] = gArray[k++];
}
Output: