0% found this document useful (0 votes)
19 views

Code Demonstration

The document describes the implementation and testing of the merge sort algorithm. It begins with an introduction to the merge sort algorithm and its working. It then discusses the time and space complexity of merge sort. Next, it provides the pseudocode for the merge sort algorithm. It also includes the code to implement merge sort in Java and generate random numbers for testing purposes. The code takes an array of 1000 random numbers as input, sorts them using merge sort, and prints the sorted output.

Uploaded by

Shreyas M
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Code Demonstration

The document describes the implementation and testing of the merge sort algorithm. It begins with an introduction to the merge sort algorithm and its working. It then discusses the time and space complexity of merge sort. Next, it provides the pseudocode for the merge sort algorithm. It also includes the code to implement merge sort in Java and generate random numbers for testing purposes. The code takes an array of 1000 random numbers as input, sorts them using merge sort, and prints the sorted output.

Uploaded by

Shreyas M
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

School of Computer Science and Engineering

Department of Computer Science and Engineering


Jain Global Campus, Kanakapura Taluk
Ramanagaram District, Karnataka, India -562112

2022-2023

A Code Demonstration Report on

“Title”

Submitted as part of assessment for the course

21CIC32 – DESIGN AND ANALYSIS OF ALGORITHM

IN

COMPUTER SCIENCE AND ENGINEERING

Submitted by

<21BTRCS073> <SHREYAS M>


<21BTRCS062> <S AMRUTHA>
<21BTRCS079> <SUHAS D >

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

The Merge Sort algorithm is a sorting algorithm that is based on the Divide and


Conquer paradigm. In this algorithm, the array is initially divided into two equal halves and then
they are combined in a sorted manner.

Working of Merge sort Algorithm


Now, let's see the working of 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.

Let the elements of array are -

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 -

Now, the array is completely sorted.

Merge sort complexity


Now, let's see the time complexity of merge sort in best case, average case, and in worst case. We
will also see the space complexity of the merge sort.

1. Time Complexity

Case Time Complexity

Best Case O(n*log n)

Average Case O(n*log n)

Worst Case O(n*log n)


o Best Case Complexity - It occurs when there is no sorting required, i.e. the array is already
sorted. The best-case time complexity of merge sort is O(n*logn).
o Average Case Complexity - It occurs when the array elements are in jumbled order that is
not properly ascending and not properly descending. The average case time complexity of
merge sort is O(n*logn).
o Worst Case Complexity - It occurs when the array elements are required to be sorted in
reverse order. That means suppose you have to sort the array elements in ascending order, but
its elements are in descending order. The worst-case time complexity of merge sort
is O(n*logn).

2. Space Complexity

Space Complexity O(n)

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?

Use the random package to generate a random number (import


random). And one helpful function random.randint(min, max) will return a number
within
the range of min and max.

def random_numbers_generator(num=1000, min=0, max=10000):


"""
Returns num of random elements
Precondition: num, min, max should be int, max should bigger than num
Example:
random_numbers_generator(num=3), return [645, 23, 7512]
random_numbers_generator(num=3, min=0, max=10), return [5, 3, 1]
random_numbers_generator(num=3, min=5, max=10), return [6, 9, 7]
"""
ALGORITHM

Algorithm for merge sort:

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;

/* 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 subarray 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++;
}
}

// Main function that sorts arr[l..r] using


// merge()
public static void sort(int arr[], int l, int r) {
if (l < r) {
// Find the middle point
int m = l + (r - l) / 2;

// Sort first and second halves


sort(arr, l, m);
sort(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}

/* A utility function to print array of size n */


static void printArray(int arr[]) {
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

public static void main(String args[]) {


int a[] = new int[1000];
Random r = new Random();
for (int i = 0; i < 1000; i++) {
a[i] = r.nextInt(10, 90);
}
for (int i = 0; i < 1000; i++) {
System.out.println(a[i]);
}

q10 ob = new q10();


ob.sort(a, 0, a.length - 1);

System.out.println("\nSorted array");
printArray(a);
}

You might also like