You are on page 1of 7

SVKM’s NMIMS

Mukesh Patel School of Technology Management & Engineering Shirpur Campus

Deparment of Computer Engineering

Instructor Manual Lab Manual Academic Year- 2023-24

Year:- II Subject:- Design & Analysis of algorithms Semester:- IV

Experiment-5

Date of Performance:- 23/01/24 Date of Submission:- 11/02/2024

Class:- B Tech CE Div-B

Student SAP Id:- 70022200382

Student Roll No:- A-160

Student Name:- samrudhi patil

AIM: Analyse and Implement the solution for Merge Sort.

Merge Sort Algorithm:

MergeSort(arr, left, right)

1. If left is less than right, then

a. Set mid as the index of the middle element of the array, i.e., (left + right) /
2

b. Recursively call MergeSort for the left half of the array, i.e.,
MergeSort(arr, left, mid)

c. Recursively call MergeSort for the right half of the array, i.e.,
MergeSort(arr, mid + 1, right)

d. Merge the sorted left and right halves using the merge function, i.e.,
merge(arr, left, mid, right)
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering Shirpur Campus

Deparment of Computer Engineering

Instructor Manual Lab Manual Academic Year- 2023-24

Year:- II Subject:- Design & Analysis of algorithms Semester:- IV

Merge(arr, left, mid, right)

1. Calculate the sizes of the two subarrays:

- n1 = mid - left + 1

- n2 = right - mid

2. Create temporary arrays L and R to hold the elements of the two subarrays

3. Copy the data from the main array arr to the temporary arrays L and R

4. Merge the elements of L and R back into the main array arr:

a. Initialize indices i, j, and k to 0

b. Compare elements of L and R one by one, and copy the smaller element to
arr

c. Increment the index of the subarray from which the element was copied
and the index of the merged array

5. Copy the remaining elements of L and R, if any, back into arr

6. The array arr is now merged and sorted

Code:

#include <iostream>

#include <vector>

using namespace std;


SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering Shirpur Campus

Deparment of Computer Engineering

Instructor Manual Lab Manual Academic Year- 2023-24

Year:- II Subject:- Design & Analysis of algorithms Semester:- IV

// Merge two sorted subarrays into one sorted array

void merge(vector<int>& arr, int left, int mid, int right) {

int n1 = mid - left + 1; // Size of left subarray

int n2 = right - mid; // Size of right subarray

// Create temporary arrays to hold the elements of the two subarrays

vector<int> L(n1);

vector<int> R(n2);

// Copy data to temporary arrays L[] and R[]

for (int i = 0; i < n1; i++)

L[i] = arr[left + i];

for (int j = 0; j < n2; j++)

R[j] = arr[mid + 1 + j];

// Merge the temporary arrays back into arr[left..right]

int i = 0; // Initial index of left subarray

int j = 0; // Initial index of right subarray

int k = left; // Initial index of merged subarray


SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering Shirpur Campus

Deparment of Computer Engineering

Instructor Manual Lab Manual Academic Year- 2023-24

Year:- II Subject:- Design & Analysis of algorithms Semester:- IV

while (i < n1 && j < n2) {

if (L[i] <= R[j]) {

arr[k] = L[i];

i++;

} else {

arr[k] = R[j];

j++;

k++;

// Copy the remaining elements of L[], if any

while (i < n1) {

arr[k] = L[i];

i++;

k++;

// Copy the remaining elements of R[], if any

while (j < n2) {


SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering Shirpur Campus

Deparment of Computer Engineering

Instructor Manual Lab Manual Academic Year- 2023-24

Year:- II Subject:- Design & Analysis of algorithms Semester:- IV

arr[k] = R[j];

j++;

k++;

// Merge Sort function

void mergeSort(vector<int>& arr, int left, int right) {

if (left < right) {

// Find the middle point

int mid = left + (right - left) / 2;

// Sort first and second halves

mergeSort(arr, left, mid);

mergeSort(arr, mid + 1, right);

// Merge the sorted halves

merge(arr, left, mid, right);

}
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering Shirpur Campus

Deparment of Computer Engineering

Instructor Manual Lab Manual Academic Year- 2023-24

Year:- II Subject:- Design & Analysis of algorithms Semester:- IV

int main() {

// Example usage

vector<int> arr = {12, 11, 13, 5, 6, 7};

int n = arr.size();

cout << "Original array: ";

for (int num : arr) {

cout << num << " ";

cout << endl;

// Perform Merge Sort

mergeSort(arr, 0, n - 1);

cout << "Sorted array: ";

for (int num : arr) {

cout << num << " ";

cout << endl;


SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering Shirpur Campus

Deparment of Computer Engineering

Instructor Manual Lab Manual Academic Year- 2023-24

Year:- II Subject:- Design & Analysis of algorithms Semester:- IV

return 0;

Output:

Time Complexity:

The time complexity of the Merge Sort algorithm implemented in the above
code is O(n log n).

Conclusion:

Hence, we implemented merge sort algorithm in C++ programming language.

You might also like