0% found this document useful (0 votes)
6 views8 pages

Program-03

The document outlines a program for sorting a set of integers using the Merge Sort algorithm, detailing its implementation in Java. It explains the divide-and-conquer technique, the algorithm's steps, and its time complexities for best, average, and worst cases, all being O(n log n). Additionally, it includes a sample code for executing the sort and measuring the time taken for varying input sizes.

Uploaded by

raviba1998
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Program-03

The document outlines a program for sorting a set of integers using the Merge Sort algorithm, detailing its implementation in Java. It explains the divide-and-conquer technique, the algorithm's steps, and its time complexities for best, average, and worst cases, all being O(n log n). Additionally, it includes a sample code for executing the sort and measuring the time taken for varying input sizes.

Uploaded by

raviba1998
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Subject Code:21CS42

Subject :Design and Analysis of Algorithm

Program-03

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. The
elements can be read from a file or can be generated using the random
number generator. Demonstrate using C++/Java how the divide-and-conquer
method works along with its time complexity analysis: worst case, average
case and best case.

Theory Concept : Merge Sort

• Merge sort is yet another sorting algorithm that falls under the category
of Divide and Conquer technique.
• It is one of the best sorting techniques that successfully build a recursive
algorithm.

In this technique, we segment a problem into two halves and solve them
individually. After finding the solution of each half, we merge them back to
represent the solution of the main problem.

Search Creators… Page 1


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

ALGORITHM

1. 1. If p<r
2. 2. Then q → ( p+ r)/2

3. 3. MERGE-SORT (A, p, q)
4. 4. MERGE-SORT ( A, q+1,r)

5. 5. MERGE ( A, p, q, r)

How Merge Sort works


Step-1: The merge sort algorithm iteratively divides an array into equal halves until
we achieve an atomic value. In case if there are an odd number of elements in an
array, then one of the halves will have more elements than the other half.

Step-2: After dividing an array into two subarrays, we will notice that it did not
hamper the order of elements as they were in the original array. After now, we will
further divide these two arrays into other halves.

Step-3: Again, we will divide these arrays until we achieve an atomic value, i.e., a
value that cannot be further divided.

Step-4: Next, we will merge them back in the same way as they were broken down.

Step-5: For each list, we will first compare the element and then combine them to
form a new sorted list.

Step-6: In the next iteration, we will compare the lists of two data values and merge
them back into a list of found data values, all placed in a sorted manner.

Search Creators… Page 2


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Example:

Search Creators… Page 3


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Analysis of Merge Sort:


Let T (n) be the total time taken by the Merge Sort algorithm.

o Sorting two halves will take at the most 2T time.


o When we merge the sorted lists, we come up with a total n-1 comparison because
the last element which is left will need to be copied down in the combined list, and
there will be no comparison.

Thus, the relational formula will be

Time Complexities:

Best Case Complexity: The merge sort algorithm has a best-case time complexity
of O(n*log n) for the already sorted array.

Average Case Complexity: The average-case time complexity for the merge sort
algorithm is O(n*log n), which happens when 2 or more elements are jumbled, i.e.,
neither in the ascending order nor in the descending order.

Worst Case Complexity: The worst-case time complexity is also O(n*log n), which
occurs when we sort the descending order of an array into the ascending order.

Space Complexity: The space complexity of merge sort is O(n).

Search Creators… Page 4


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

PROGRAM

import java.util.Random;
import java.util.Scanner;

public class Merge_Sort {


public static int SIZE = 7000;

public static void main(String[] args)throws


ArrayIndexOutOfBoundsException {
int a[] = new int[SIZE];
System.out.println("Enter total number of elements for Sorting");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Random m = new Random();
for(int i=0;i<n;i++) {
a[i] = m.nextInt(10)+1;
}
System.out.println("\n The Elements Before Sorting...");
for(int i=0;i<n;i++) {
System.out.println(""+a[i]);
}
long start_time,end_time;
start_time = System.nanoTime();
Merge_Sort(a,0,n-1);
end_time = System.nanoTime();

Search Creators… Page 5


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

System.out.println("\n The Elements After Sorting....");


for(int i=0;i<n;i++) {
System.out.println(""+a[i]);
}
System.out.println("\n The Time Required for Sorting"+n+"Numbers
is:"+(end_time-start_time)+"ns");
}
public static void Merge_Sort(int a[],int low,int high) {
int mid;
if(low<high) {
mid = (low+high)/2;
Merge_Sort(a,low,mid);
Merge_Sort(a,mid+1,high);
combine(a,low,mid,high);
}
}
public static void combine(int a[],int low,int mid,int high) {
int i,j,k;
int[] temp;
temp = new int[7000];
k = low;
i = low;
j = mid+1;
while(i<=mid&&j<=high) {
if(a[i]<=a[j]) {
temp[k]=a[i];
i++;
Search Creators… Page 6
21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

k++;
}
else {
temp[k] = a[j];
j++;
k++;
}

}
while(i<=mid) {
temp[k] = a[i];
i++;
k++;
}
while(j<=high) {
temp[k] = a[j];
j++;
k++;
}
for(k = low; k<=high; k++) {
a[k] = temp[k];
}
}

Search Creators… Page 7


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

OUTPUT

Search Creators… Page 8

You might also like