LAB PRACTICE SHEET AND SOLUTIONS
(INSERTION SORT)
Date: 01-08-2024 Venue: AB3 - 311
Reg No.: 22BCE1032 Name: Ronak Chordia
Question:
Translate the merge-sort algorithm (A) discussed in the class into a program (P) and
execute the same for the following inputs: (i):(0,1,2,3,4,5) (ii)(5, 5.5, 6, 3.723, 1.23,
8.88).
Aim:
To successfully perform merge sort on n numbers.
Pseudocode:
function merge(L, R, arr):
mid = (L + R) / 2
i = L
j = mid
res = empty list
while i < mid and j < R:
if arr[i] < arr[j]:
append arr[i] to res
i = i + 1
else:
append arr[j] to res
j = j + 1
while i < mid:
append arr[i] to res
i = i + 1
while j < R:
append arr[j] to res
j = j + 1
for k from L to R - 1:
arr[k] = res[k - L]
function mergesort(L, R, arr):
if L < R - 1:
mid = (L + R) / 2
mergesort(L, mid, arr)
mergesort(mid, R, arr)
merge(L, R, arr)
Coding:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;
void merge(int L, int R, vector<int>& arr) {
int mid = (L + R) / 2;
int i = L;
int j = mid;
vector<int> res;
while (i < mid && j < R) {
if (arr[i] < arr[j]) {
res.push_back(arr[i++]);
} else {
res.push_back(arr[j++]);
}
}
while (i < mid) {
res.push_back(arr[i++]);
}
while (j < R) {
res.push_back(arr[j++]);
}
for (int k = L; k < R; k++) {
arr[k] = res[k - L];
}
}
void mergesort(int L, int R, vector<int>& arr) {
if (L < R - 1) {
int mid = (L + R) / 2;
mergesort(L, mid, arr);
mergesort(mid, R, arr);
merge(L, R, arr);
}
}
int main() {
vector<int> iterations = {500, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000,
9000, 10000};
vector<int> arr(10000);
// Generate 10000 random integers
srand(time(0));
for (int i = 0; i < 10000; i++) {
arr[i] = rand();
}
for (int iter : iterations) {
vector<int> slice(arr.begin(), arr.begin() + iter);
int start = clock();
mergesort(0, slice.size(), slice);
int end = clock();
double time_taken = (double(end - start) /
double(CLOCKS_PER_SEC))*1000000000;
cout << "Time taken to perform merge sort on " << iter << " elements is: "
<< fixed << time_taken << "ns" << endl;
}
return 0;
}
Test case:
Input:
ii)
Output:
ii)
Table:
Number of Iterations Time
500 0.000408
1000 0.000719
2000 0.002305
3000 0.002305
4000 0.002922
5000 0. 0.003877
6000 0.004679
7000 0.005280
8000 0. 005946
9000 0.007199
10000 0.008142
Analysis Graph:
T(n) of the code:
Best Case:
0(logn)
Worst Case:
O(nlogn)
Average Case:
O(logn)
Conclusion:
Merge sort was successfully performed with various combinations of numbers
Question 2:
Modify the Merge-sort algorithm (discussed in the class) such that the
algorithm takes the input as words (of different lengths) and arranges them in
an alphabetical order. Your words will have both lower-case letters as well as
the upper-case letters. Compute the time complexity t(n) of your algorithm in
an experimental approach. Compare this algorithm with that of the algorithm
which takes n numbers as inputs and decides which consumes minimum time
Code:
#include <iostream>
#include <ctime>
using namespace std;
}Output and Time Complexity comparison:
Question 3:
The Merge-sort algorithm (discussed in the class) works by partitioning the input
array A recursively into two halves. Here, the partition is based on the position in
the array. Instead, design a new algorithm A’ where portioning is based on the
values in the input array. Compare the performance of A’ with that of A.
Code:
#include <iostream>
#include <ctime>
using namespace std;
Output and Time Complexity comparison: