LPS 1&2
LPS 1&2
LPS-1&2
Eniya Sre AG-22BPS1002
1)Modify the Merge-sort algorithm and implement the program such that the algorithm
functions for the string-based input-outputs.
Tasks:
(i) That is, your algorithm should sort the given n words and arrange them in an
increasing order.
(ii) Your program also gives total number of comparisons used in merging process.
(iii) Your program should also give total number of recursive calls used in the
program.
(iv) Your program should also count total number of cases in which the right subarray
directly placed in the result during the merging procedure.
(v) Your program should also count total number of cases in which the left subarray
directly placed in the result during the merging procedure.
Code:
#include <iostream>
#include <string>
using namespace std;
int r1=0;
int c=0;
int l1=0;
int r2=0;
}
else{
temp[k]=a[j];
j++;k++;
}
}
while(i<=m)
{ l1++;
temp[k]=a[i];
i++;
k++;
}
while(j<=r){
temp[k]=a[j];
j++;k++;
r2++;
}
for(int q=l;q<=r;q++){
a[q]=temp[q];
}
if(l<r){
int mid=(l+r)/2;
r1++;
ms(a,l,mid);
ms(a,mid+1,r);
merge(a,l,mid,r);
}
}
int main() {
string a[5]={"the","girl","is","blue","pink"};
ms(a,0,4);
cout<<"The sorted array:";
for(int p=0;p<5;p++)
{
cout<<a[p]<<" ";
}
cout<<endl;
cout<<"No of comparisons:"<<c<<endl;
cout<<"No of recursive calls:"<<r1<<endl;
cout<<"Total number of cases in which the left subarray directly
placed in the result: "<<l1<<endl;
cout<<"Total number of cases in which the right subarray directly
placed in the result: "<<r2;
return 0;
}
Output:
eniya@DESKTOP-PDU1O0D:~/Documents/DAA$ ./mergesort
The sorted array:blue girl is pink the
No of comparisons:8
No of recursive calls:4
Total number of cases in which the left subarray directly placed in the result: 3
Total number of cases in which the right subarray directly placed in the result: 1
2)Modify the Merge-sort algorithm and implement the program such that the algorithm functions
for the string-based input-outputs. That is, your algorithm should sort the given n words and arrange
them in an increasing order. You can choose any measure for the ordering purpose. Compute the
time-complexity of your algorithm in an experimental approach. Compare this algorithm with that of
the algorithm which takes n numbers as input and decide which works efficiently.
Code:
#include <iostream>
#include <vector>
#include <chrono>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;
// Function prototypes
void mergeSortStrings(vector<string>& arr, int l, int r);
void mergeSortNumbers(vector<int>& arr, int l, int r);
void mergeStrings(vector<string>& arr, int l, int m, int r);
void mergeNumbers(vector<int>& arr, int l, int m, int r);
int main() {
srand(time(0));
int n = 10000;
int strLength = 10;
cout << "Time taken to sort " << n << " strings: " <<
durationStr.count() << " seconds" << endl;
cout << "Time taken to sort " << n << " numbers: " <<
durationNum.count() << " seconds" << endl;
return 0;
}
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (left[i] <= right[j]) {
arr[k] = left[i];
i++;
} else {
arr[k] = right[j];
j++;
}
k++;
}
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (left[i] <= right[j]) {
arr[k] = left[i];
i++;
} else {
arr[k] = right[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = left[i];
i++;
k++;
}
Output:
eniya@DESKTOP-PDU1O0D:~/Documents/DAA$ ./mergesort
Time taken to sort 10000 strings: 0.0394124 seconds
Time taken to sort 10000 numbers: 0.0224423 seconds
3)Modify the Merge-sort algorithm and implement the same to find the total number of
comparisons used in the Merge( ) procedure.
Code:
#include <iostream>
#include <string>
using namespace std;
int c=0;
}
else{
temp[k]=a[j];
j++;k++;
}
}
while(i<=m)
{
temp[k]=a[i];
i++;
k++;
}
while(j<=r){
temp[k]=a[j];
j++;k++;
}
for(int q=l;q<=r;q++){
a[q]=temp[q];
}
if(l<r){
int mid=(l+r)/2;
ms(a,l,mid);
ms(a,mid+1,r);
merge(a,l,mid,r);
}
}
int main() {
int a[5]={9,4,2,3,7};
ms(a,0,4);
cout<<"The sorted array:";
for(int p=0;p<5;p++)
{
cout<<a[p]<<" ";
}
cout<<endl;
cout<<"No of comparisons:"<<c<<endl;
return 0;
}
Output:
eniya@DESKTOP-PDU1O0D:~/Documents/DAA$ ./mergesort
The sorted array:2 3 4 7 9
No of comparisons:7
4) Merge-sort works by portioning the input array A recursively into two sub-array’s each of length
approximately |A| / 2. Instead, partition the input array into two sub-array’s A1 and A2 such that the
|A1| = x, |A2| = y where x and y are two integers within the range (1,n-1) so that, for each pair (x, y),
we will have a partition of A. For example, If (x, y) is (3, n−3), that means the input array A is
partitioned into arrays A1 with |A1| = 3 and A2 with |A2| = (n−3). If A is of length n, we will have n-1
such pairs of values for (x, y) : (1, n-1),(2, n − 2), . . .(n-1, 1) Conduct n-1 experiments, E1, E2, . . . , En
such that E1 partition’s A for the pair (1, n), E2 partitions for the pair (2, n − 2) and so on. Compute
the time complexity T(P) for each of the experiment and decide which partitions consumes minimum
time for execution. For all the experiments, use the same input. Note that the time complexity
should be computed using code and the code will use system clock for this purpose.
Code:
#include <iostream>
#include <vector>
#include <chrono>
#include <cstdlib>
#include <ctime>
#include <limits>
using namespace std;
vector<string> left(n1);
vector<string> right(n2);
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (left[i] <= right[j]) {
arr[k] = left[i];
i++;
} else {
arr[k] = right[j];
j++;
}
k++;
}
int main() {
srand(time(0));
int n = 1000;
int strLength = 10;
vector<string> arr = generateRandomStrings(n, strLength);
cout << "Time taken for partition (" << x << ", " << y << "): "
<< duration.count() << " seconds" << endl;
cout << "Best partition: (" << bestPartition << ", " << n -
bestPartition << ")" << endl;
cout << "Minimum time: " << minTime << " seconds" << endl;
return 0;
}
Output:
eniya@DESKTOP-PDU1O0D:~/Documents/DAA$ ./mergesort
Time taken for partition (1, 999): 0.0020549 seconds
Time taken for partition (2, 998): 0.0034848 seconds
Time taken for partition (3, 997): 0.0036394 seconds
Time taken for partition (4, 996): 0.0021751 seconds
Time taken for partition (5, 995): 0.0017763 seconds
Time taken for partition (6, 994): 0.0016006 seconds
Time taken for partition (7, 993): 0.0016577 seconds
Time taken for partition (8, 992): 0.0018177 seconds
Time taken for partition (9, 991): 0.0017138 seconds
Time taken for partition (10, 990): 0.0018473 seconds
Time taken for partition (11, 989): 0.0014426 seconds
Time taken for partition (12, 988): 0.0014334 seconds
Time taken for partition (13, 987): 0.0021055 seconds
Time taken for partition (14, 986): 0.0019093 seconds
.
.
.
Time taken for partition (996, 4): 0.0027366 seconds
Time taken for partition (997, 3): 0.00332 seconds
Time taken for partition (998, 2): 0.0020415 seconds
Time taken for partition (999, 1): 0.0018007 seconds
Best partition: (40, 960)
Minimum time: 0.0011242 seconds
5) 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 partitioning is based on the values in the input array. Compare the performance
of A’ with that of A.
Code:
#include <iostream>
#include <vector>
#include <chrono>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
vector<int> left(n1);
vector<int> right(n2);
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (left[i] <= right[j]) {
arr[k] = left[i];
i++;
} else {
arr[k] = right[j];
j++;
}
k++;
}
while (i <= j) {
while (arr[i] < pivot) {
i++;
}
while (arr[j] > pivot) {
j--;
}
if (i <= j) {
swap(arr[i], arr[j]);
i++;
j--;
}
}
if (l < j) {
valueBasedMerge(arr, l, j);
}
if (i < r) {
valueBasedMerge(arr, i, r);
}
}
}
vector<int> generateRandomNumbers(int n) {
vector<int> arr(n);
for (int i = 0; i < n; i++) {
arr[i] = rand() % 100000;
}
return arr;
}
int main() {
srand(time(0));
int n = 10000;
cout << "Time taken for traditional merge sort (A): " <<
durationA.count() << " seconds" << endl;
cout << "Time taken for value-based merge sort (A'): " <<
durationA_prime.count() << " seconds" << endl;
return 0;
}
Output:
eniya@DESKTOP-PDU1O0D:~/Documents/DAA$ ./mergesort
Time taken for traditional merge sort (A): 0.0154983 seconds
Time taken for value-based merge sort (A'): 0.003862 seconds
6) Run the merge-sort algorithm for different inputs i1, i2, ..i10, where i1 is an array of n single digit
numbers, i2 is an array of n two-digit numbers, ..., i10n is an array of n 10-digit numbers. For each of
the above ten experiments, compute the time complexity T(P). Based on the experiment, compute
the time taken by your machine to compare two n − digit numbers.
Code:
#include <iostream>
#include <vector>
#include <chrono>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
int main() {
srand(time(0));
int n = 10000;
for (int digitLength = 1; digitLength <= 10; digitLength++) {
vector<long long> arr = generateRandomNumbers(n, digitLength);
auto start = chrono::high_resolution_clock::now();
mergeSort(arr, 0, arr.size() - 1);
auto end = chrono::high_resolution_clock::now();
chrono::duration<double> duration = end - start;
cout << "Time taken for sorting array of " << digitLength << "-
digit numbers: " << duration.count() << " seconds" << endl;
}
return 0;
}
Output:
eniya@DESKTOP-PDU1O0D:~/Documents/DAA$ ./mergesort
Time taken for sorting array of 1-digit numbers: 0.0154828 seconds
Time taken for sorting array of 2-digit numbers: 0.0114753 seconds
Time taken for sorting array of 3-digit numbers: 0.0102177 seconds
Time taken for sorting array of 4-digit numbers: 0.012974 seconds
Time taken for sorting array of 5-digit numbers: 0.0134225 seconds
Time taken for sorting array of 6-digit numbers: 0.0116341 seconds
Time taken for sorting array of 7-digit numbers: 0.0109186 seconds
Time taken for sorting array of 8-digit numbers: 0.0121606 seconds
Time taken for sorting array of 9-digit numbers: 0.0122075 seconds
Time taken for sorting array of 10-digit numbers: 0.0093065 seconds
7) Read a file which is containing names of the students. Apply merge-sort technique to sort the last
character of each student’s name in reverse lexicographical ordering.
Code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
Students.txt
eniya
ilakiya
riya
subhikha
midhuna
navya
adith
Output:
eniya@DESKTOP-PDU1O0D:~/Documents/DAA$ ./mergesort
adith
eniya
ilakiya
riya
subhikha
midhuna
navya
8)
Code:
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
int n;
cout << "Enter the size of the matrix (N): ";
cin >> n;
return 0;
}
Output:
eniya@DESKTOP-PDU1O0D:~/Documents/DAA$ ./mergesort
Enter the size of the matrix (N): 3
Enter the elements of the matrix:
11 2 4
456
10 8 -12
The absolute difference of the sums across the two main diagonals is: 15
9)Given n integers in an array, sort the array elements alternatively in such a way that the first
element is first maximum and second element is first minimum and so on.
Examples :
Input : arr[] = {7, 1, 2, 3, 4, 5, 6}
Output : 7 1 6 2 5 3 4
Input : arr[] = {1, 6, 9, 4, 3, 7, 8, 2}
Output : 9 1 8 2 7 3 6 4
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> arr = {7, 1, 2, 3, 4, 5, 6}; // Example input
vector<int> result = rearrangeAlternately(arr);
cout << "Rearranged array: ";
for (int num : result) {
cout << num << " ";
}
cout << endl;
return 0;
}
Output:
eniya@DESKTOP-PDU1O0D:~/Documents/DAA$ ./mergesort
Rearranged array: 7 1 6 2 5 3 4
10) Given an array of n distinct elements and a number x, arrange array elements according
to the absolute difference with x, i. e., an element having minimum difference comes first,
and so on.
Note: If two or more elements are at equal distance arrange them in the same sequence as in
the given array.
Examples :
Input : arr[] : x = 7, arr[] = {10, 5, 3, 9, 2} Output : arr[] = {5, 9, 10, 3, 2}
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Element {
int value;
int index;
};
int main() {
vector<int> arr = {10, 5, 3, 9, 2};
int x = 7;
arrangeByDifference(arr, x);
return 0;
}
Output:
eniya@DESKTOP-PDU1O0D:~/Documents/DAA$ ./mergesort
Arranged array: 5 9 10 3 2