Lab 2 RC
Lab 2 RC
(INSERTION SORT)
Date: 01-08-2024 Venue: AB3 - 311
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:
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]
int main() {
vector<int> iterations = {500, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000,
9000, 10000};
vector<int> arr(10000);
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:
Best Case:
0(logn)
Worst Case:
O(nlogn)
Average Case:
O(logn)
Conclusion:
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>
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>