Code Demonstration
Code Demonstration
2022-2023
“Title”
IN
Submitted by
Dr. Manjunath C R
Associate Professor
Department of Computer Science and Engineering
School of Computer Science and Engineering
Faculty of Engineering and Technology
JAIN (Deemed-to-be University)
2022-2023
School of Computer Science and Engineering
Department of Computer Science and Engineering
Jain Global Campus, Kanakapura Taluk
Ramanagara District, Karnataka, India -562112
CERTIFICATE
This is to certify that the report entitled “title of the report” is carried out by <SHREYAS
M(21BTRCS073)> <S AMRUTHA(21BTRCS062)> <SUHAS D SHETTY (21BTRCS079)> , a
bonafide student of Bachelor of Technology at the Faculty of Engineering and Technology,
JAIN (Deemed-to-be University) in partial fulfilment for the course 21CIC32 - DESIGN AND
ANALYSIS OF ALGORITHMS of the degree of Bachelor of Technology in Computer
Science and Engineering, during the academic year 2022-2023.
Dr Manjunath CR
Associate Professor
Dept. of CSE School of CSE
Faculty of Engineering & Technology
JAIN (Deemed-to- be University).
TABLE OF CONTENTS
SL.NO TOPICS PAGE
INTRODUCTION TO MERGE SORT ALGORITHM
To understand the working of the merge sort algorithm, let's take an unsorted array. It will be easier
to understand the merge sort via an example.
According to the merge sort, first divide the given array into two equal halves. Merge sort keeps
dividing the list into equal parts until it cannot be further divided.
As there are eight elements in the given array, so it is divided into two arrays of size 4.
Now, again divide these two arrays into halves. As they are of size 4, so divide them into
new arrays of size 2.
Now, again divide these arrays to get the atomic value that cannot be further divided.
Now, combine them in the same manner they were broken.
In combining, first compare the element of each array and then combine them into another array in
sorted order.
So, first compare 12 and 31, both are in sorted positions. Then compare 25 and 8, and in the list of
two values, put 8 first followed by 25. Then compare 32 and 17, sort them and put 17 first followed
by 32. After that, compare 40 and 42, and place them sequentially.
In the next iteration of combining, now compare the arrays with two data values and merge them into
an array of found values in sorted order.
Now, there is a final merging of the arrays. After the final merging of above arrays, the array will
look like -
1. Time Complexity
2. Space Complexity
Stable YES
o The space complexity of merge sort is O(n). It is because, in merge sort, an extra variable is
required for swapping
IMPLEMENTATION
You need to implement and use the sorting algorithms you learn from the class to sort
1000 random elements. And for the testing step, you need to implement a random
number generator function and test your algorithms by following the steps listed below.
1. Call the random number generator function and generate 1000 numbers.
2. Pass random numbers array into the sorting algorithms you implemented.
3. Check how the algorithms work.
How to Implement random numbers generator?
step 1: start
step 2: declare array and left, right, mid variable
step 3: perform merge function.
if left > right
return
mid= (left+right)/2
mergesort(array, left, mid)
mergesort(array, mid+1, right)
merge(array, left, mid, right)
step 4: Stop
Time Complexity
Code
import java.util.*;
public class q10 {
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;
System.out.println("\nSorted array");
printArray(a);
}