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

DSA Lab Manual (Merge Sort)

The document is a lab manual for implementing the Merge Sort algorithm in C++. It outlines the algorithm's theory, time and space complexities, provides a C++ code implementation, and includes a procedure for executing the program. The conclusion emphasizes Merge Sort's efficiency and stability, making it suitable for large datasets.

Uploaded by

Irfan Ul Haq
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)
21 views3 pages

DSA Lab Manual (Merge Sort)

The document is a lab manual for implementing the Merge Sort algorithm in C++. It outlines the algorithm's theory, time and space complexities, provides a C++ code implementation, and includes a procedure for executing the program. The conclusion emphasizes Merge Sort's efficiency and stability, making it suitable for large datasets.

Uploaded by

Irfan Ul Haq
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

Data Structures and Algorithms Lab Manual

Experiment: Merge Sort Algorithm


Objective
To understand and implement the Merge Sort algorithm, a divide-and-conquer sorting technique,
in C++.
Theory
Merge Sort is a stable and comparison-based sorting algorithm that follows the divide-and-
conquer approach. The algorithm works as follows:
1. Divide: Split the array into two halves until each sub-array contains a single element.
2. Conquer: Recursively sort the sub-arrays.
3. Combine: Merge the sorted sub-arrays to produce a single sorted array.
Time Complexity:
 Best Case: O(n log n)
 Worst Case: O(n log n)
 Average Case: O(n log n)
Space Complexity: O(n) (due to auxiliary arrays used in merging)
Algorithm
1. Define a function mergeSort to divide the array into halves.
2. Define a function merge to combine two sorted arrays into a single sorted array.
3. Recursively apply mergeSort to both halves of the array.
4. Use the merge function to combine the sorted halves.

C++ Code Implementation


#include <iostream>
using namespace std;

// Function to merge two sub-arrays


void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;

// Create temporary arrays


int L[n1], R[n2];

// Copy data to temp 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 temp arrays back into arr[left..right]


int i = 0; // Initial index of the first sub-array
int j = 0; // Initial index of the second sub-array
int k = left; // Initial index of the merged sub-array

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) {
arr[k] = R[j];
j++;
k++;
}
}

// Function to implement merge sort


void mergeSort(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;

// Sort the first and second halves


mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);

// Merge the sorted halves


merge(arr, left, mid, right);
}
}

// Function to print the array


void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}

// Main function
int main() {
int arr[] = {38, 27, 43, 3, 9, 82, 10};
int arr_size = sizeof(arr) / sizeof(arr[0]);

cout << "Given array is:\n";


printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

cout << "\nSorted array is:\n";


printArray(arr, arr_size);
return 0;
}
Procedure
1. Write the code in your preferred IDE or text editor.
2. Compile the program using a C++ compiler.
3. Execute the program and observe the output.
4. Test the code with different input arrays to validate the algorithm.

Output
Example Input:
Given array is:
38 27 43 3 9 82 10
Example Output:
Sorted array is:
3 9 10 27 38 43 82

Practice Questions
1. Analyze the time complexity of the Merge Sort algorithm for arrays of size 4, 8, and 16.
2. Modify the code to sort an array of floating-point numbers.
3. Implement Merge Sort iteratively instead of recursively.

Conclusion
Merge Sort is an efficient, stable, and widely used sorting algorithm with a consistent time
complexity of O(n log n). It is especially useful for large datasets where stability and predictable
performance are critical.

You might also like