0% found this document useful (0 votes)
54 views3 pages

Merge Sort 23

The document describes using merge sort to sort a set of integers. It generates random integers of size n greater than 5000, sorts them using merge sort, and records the time taken. It plots a graph of time taken versus n. The code provided implements merge sort in Java, recursively dividing the array into halves and merging the sorted halves. It analyzes the time complexity of merge sort as worst case, average case, and best case.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views3 pages

Merge Sort 23

The document describes using merge sort to sort a set of integers. It generates random integers of size n greater than 5000, sorts them using merge sort, and records the time taken. It plots a graph of time taken versus n. The code provided implements merge sort in Java, recursively dividing the array into halves and merging the sorted halves. It analyzes the time complexity of merge sort as worst case, average case, and best case.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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. Plot a graph of the time taken versus n on graph
sheet. The elements can be read from a file or can be generated using the
random number generator. 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;
public class MergeSort {
static final int MAX = 100000;
static int[] a = new int[MAX];
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter Max array size: ");
int n = input.nextInt();
Random random = new Random();
for (int i = 0; i < n; i++)
a[i] = random.nextInt(10000);
System.out.println("Input Array is:");
for (int i = 0; i < n; i++)
System.out.println(a[i] + " ");
long startTime = System.nanoTime();
MergeSortAlgorithm(0, n - 1);
long stopTime = System.nanoTime();
long elapsedTime = stopTime - startTime;
System.out.println("********Sorted Array is***********");
for (int i = 0; i < n; i++)
System.out.println(a[i] + " ");
System.out.println("Time Complexity (ms) for n = " +
n + " is : " + (double) elapsedTime / 1000000);
input.close();
}

public static void MergeSortAlgorithm(int low, int high) {


int mid;
if (low < high) {
mid = (low + high) / 2;
MergeSortAlgorithm(low, mid);
MergeSortAlgorithm(mid + 1, high);
Merge(low, mid, high);
}
}

public static void Merge(int low, int mid, int high) {


int[] b = new int[MAX];
int i, j, k,h;
i =k= low;
j = mid + 1;
while ((i <= mid) && (j <= high))
if (a[i] < a[j])
b[k++] = a[i++];
else
b[k++] = a[j++];

if (i > mid)
for (h = j; h <= high; h++)
b[k++] = a[h];
else
for (h = i; h <= mid; h++)
b[k++] = a[h];

for (k = low; k <= high; k++)


a[k] = b[k];
}
}

You might also like