You are on page 1of 17

CSE 2012 DAA LAB3 ASSIGNEMNT

DIVIDE & CONQUER ALGORITHM


SUBMISSION BY: Aryan Bhardwaj
REGISTER NUMBER: 20BCE1908
DATE: 23.01.2022
SLOT: L9-L10

1) Let A be a two-dimensional matrix of size nxn (n>2), Write an algorithm (Divide and
Conquer) and subsequent program code to display the maximum element of diagonal
elements of A and the minimum element of anti-diagonal elements of A.
CODE –
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
void diagonal_merge(int a[100][100], int f, int m, int l)
{
int n1 = m - f + 1; //middle - first
int n2 = l - m; // last - middle
int left[n1] , right[n2]; //left subarray //right subarray
for (int i = 0; i < n1; i++)
{
left[i] = a[f + i][f + i];
}
for (int i = 0; i < n2; i++)
{
right[i] = a[m + 1 + i][m + 1 + i];
}
int i = 0, j = 0, k = f;
while (i < n1 && j < n2)
{
if (left[i] <= right[j])
{
a[k][k] = left[i];
i++;
k++;
}
else
{
a[k][k] = right[j];
j++;
k++;
}
}
while (i < n1)
{
a[k][k] = left[i];
i++;
k++;
}
while (j < n2)
{
a[k][k] = right[j];
j++;
k++;
}
}
void mergesort_diagonal(int a[100][100], int f, int l)
{
if (f < l)
{
int m = f + (l - f) / 2;
mergesort_diagonal(a, f, m);
mergesort_diagonal(a, m + 1, l);
diagonal_merge(a, f, m, l);
}
}
void display_array(int a[100][100], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << a[i][j] << "\t";
}
cout<<"\n";
}
}
int main()
{
int i, n;
cout<<"Enter the number of rows & columns = ";
cin >> n;
int arr[100][100];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> arr[i][j];
}
}
int len_array = n-1;
mergesort_diagonal(arr, 0, len_array);
printf("\nThe Sorted diagonal matrix is = \n");
display_array(arr, n);
return 0;
}

OUTPUT –
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 <stdio.h>
#include <string>
#include <iostream>
using namespace std;
void merge(string array[], int const left, int const mid, int const right)
{
auto const arr1 = mid - left + 1;
auto const arr2 = right - mid;
auto *leftArray = new
string[arr1];
auto *rightArray = new string[arr2];
for (auto i = 0; i < arr1; i++)
leftArray[i] = array[left + i];
for (auto j = 0; j < arr2; j++)
rightArray[j] = array[mid + 1 + j];

auto indexOfSubArrayOne = 0,
indexOfSubArrayTwo = 0;
int indexOfMergedArray = left;
while (indexOfSubArrayOne < arr1 && indexOfSubArrayTwo < arr2)
{
if (leftArray[indexOfSubArrayOne] <= rightArray[indexOfSubArrayTwo])
{
array[indexOfMergedArray] = leftArray[indexOfSubArrayOne];
indexOfSubArrayOne++;
}
else
{
array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo];
indexOfSubArrayTwo++;
}
indexOfMergedArray++;
}
while (indexOfSubArrayOne < arr1)
{
array[indexOfMergedArray] = leftArray[indexOfSubArrayOne];
indexOfSubArrayOne++;
indexOfMergedArray++;
}
while (indexOfSubArrayTwo < arr2)
{
array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo];
indexOfSubArrayTwo++;
indexOfMergedArray++;
}
}
void mergeSort(string array[], int const first, int const last)
{
if (first >=last)
return;

auto mid = first + (last - first) / 2;


mergeSort(array, first, mid);
mergeSort(array, mid + 1, last);
merge(array, first, mid, last);
}

void display_Array(string A[], int size)


{
for (auto i = 0; i < size; i++)
cout << A[i] << " ";
}

int main()
{
int n;
cout<<"Enter the number of strings you want = ";
cin>>n;
string arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout << "The current array = ";
display_Array(arr, n);

mergeSort(arr, 0, n - 1);

cout << "\nSorted array =


"; display_Array(arr, n);
return 0;
}
OUTPUT –
3) Modify the Merge-sort algorithm and implement the same to find the total number
of comparisons used in merging procedure.
CODE –
#include <stdio.h>
#include <stdlib.h>
int count;
void merge(int a[],int low,int mid,int high)
{
int b[high-low+1];
for(int i=low;i<=high;i+
+)
{
b[i]=a[i];
}
int i=low,j=mid+1,k=low;
while(i<=mid && j<=high)
{
if(b[i]<=b[j])
{
a[k]=b[i];
k=k+1;
i=i+1;
count=count+1;
}
else
{
a[k]=b[j];
k=k+1;
j=j+1;
count=count+1;
}
}
while(i<=mid)
{
a[k]=b[i];
k=k+1;
i=i+1;
}
while(j<=high)
{
a[k]=b[j];
k=k+1;
j=j+1;
}
}
void mergesort(int a[],int low,int high)
{
int m;
if (low<high){
m=(low+high)/2;
mergesort(a,low,m);
mergesort(a,m+1,high);
merge(a,low,m,high);
}
}
int main()
{
int n;
printf("Enter the number of elements = ");
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
mergesort(arr,0,n-1);
printf("The array after sorting = ");
for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
printf("\nComaprisons made = %d" , count);
return 0;
}

OUTPUT –
4) 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 t(P). Based on the
experiment, compute the time taken by your machine to compare two n − digit numbers.
CODE –
#include <iostream>
#include <algorithm>
#include <chrono>
#include <vector>
using namespace std;
using namespace std::chrono;
void merge(int array[], int const left, int const mid, int const right)
{
auto const arr1 = mid - left + 1;
auto const arr2 = right - mid;
auto *leftArray = new int[arr1];
auto *rightArray = new int[arr2];
for (auto i = 0; i < arr1; i++)
leftArray[i] = array[left + i];
for (auto j = 0; j < arr2; j++)
rightArray[j] = array[mid + 1 + j];

auto indexOfSubArrayOne = 0,
indexOfSubArrayTwo = 0;
int indexOfMergedArray = left;
while (indexOfSubArrayOne < arr1 && indexOfSubArrayTwo < arr2)
{
if (leftArray[indexOfSubArrayOne] <= rightArray[indexOfSubArrayTwo])
{
array[indexOfMergedArray] = leftArray[indexOfSubArrayOne];
indexOfSubArrayOne++;
}
else
{
array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo];
indexOfSubArrayTwo++;
}
indexOfMergedArray++;
}
while (indexOfSubArrayOne < arr1)
{
array[indexOfMergedArray] = leftArray[indexOfSubArrayOne];
indexOfSubArrayOne++;
indexOfMergedArray++;
}
while (indexOfSubArrayTwo < arr2)
{
array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo];
indexOfSubArrayTwo++;
indexOfMergedArray++;
}
}
void mergeSort(int array[], int const first, int const last)
{
if (first >= last)
return;

auto mid = first + (last - first) / 2;


mergeSort(array, first, mid);
mergeSort(array, mid + 1, last);
merge(array, first, mid, last);
}
void display(int array[], int size)
{
for (auto i = 0; i < size; i++)
cout << array[i] << " ";
}
int main()
{
int n;
cout<<"Enter the number of elements = ";
cin>>n;
int arr[n];
for(auto i=0;i<n;i++)
{
cin>>arr[i];
}
cout << "The current array = ";
display(arr, n);
auto start = high_resolution_clock::now();
mergeSort(arr, 0, n - 1);
auto stop = high_resolution_clock::now();
cout << "\nThe Sorted array is = ";
display(arr, n);
vector<int> values(10000);
sort(values.begin(), values.end());
auto duration = duration_cast<nanoseconds>(stop - start);
cout << "\nTime taken by function = " << duration.count() << "
nanoseconds" << endl;
return 0;
}
OUTPUT –
5) Merge-sort algorithm (discussed in the class) works by portioning the input array A
recursively into two halves. Instead, partition the input array into two pieces A1 and A2
such that the |A1| = x, |A2| = y. So, 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
length 3 and A2 with length (n−3). If A is of length n, we will have n different pairs of
values for (x, y) : (1, n),(2, n − 1), . . .(n, 1) Conduct n experiments, E1, E2, . . . , En such that
E1 partitions A for the pair (1, n), E2 partitions for the pair (2, n − 2) and so on. Compute
the t(P) for each of the experiment and decide which partitions consumes minimum time
for execution. For all the experiments, use the same input.
CODE –
#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;
void merge(int array[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int left[n1], right[n2];
for (i = 0; i < n1; i++)
left[i] = array[l +
i]; for (j = 0; j < n2; j+
+)
right[j] = array[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (left[i] <= right[j])
{
array[k] = left[i];
i++;
}
else
{
array[k] = right[j];
j++;
} k+
+;
}
while (i < n1)
{
array[k] = left[i];
i++;
k++;
}
while (j < n2)
{
array[k] = right[j];
j++;
k++;
}
}
void mergeSort(int array[], int l, int r)
{
int m;
if (l < r)
{
int m = l + (r - l) / 2;
mergeSort(array, l, m);
mergeSort(array, m + 1, r);
merge(array, l, m, r);
}
}
void display(int array[], int size)
{
for (int i = 0; i < size; i++)
cout << array[i] << " ";
cout << endl;
}
void count_mergesort_time(int *arr, int l, int r)
{
int m;
clock_t time;
if (l < r)
{
for (int i = 0; i < r - l; i++)
{
auto start = high_resolution_clock::now();
int m = l + i;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<nanoseconds>(stop - start);
cout << "Sorted Array using mid value = " << i << " :- ";
display(arr, r + 1);
cout << "Time taken = " << duration.count() << " nanoseconds" <<
endl;
}
}
}
int main()
{
int n;
cout << "Enter the number of elements = ";
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int len = n-1;
count_mergesort_time(arr, 0,
len);
return 0;

OUTPUT –
6) 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>
using namespace std;
void merge(char a[], int first, int mid, int last)
{
int i, j, k;
int n1 = mid - first + 1;
int n2 = last - mid;
char arr_left[70], arr_right[70];
for (i = 0; i < n1; i++)
{
arr_left[i] = a[first + i];
}
for (j = 0; j < n2; j++)
{
arr_right[j] = a[mid + 1 + j];
}
i = 0, j = 0, k = first;
while (i < n1 && j < n2)
{
if (tolower(arr_right[j]) > tolower(arr_left[i])) //to lowercase all
characters
{
a[k] = arr_right[j];
j++;
}
else
{
a[k] = arr_left[i];
i++;
}
k++;
}
while (i < n1)
{
a[k] = arr_left[i];
i++;
k++;
}
while (j < n2)
{
a[k] = arr_right[j];
j++;
k++;
}
}
void MergeSort(char arr[], int first, int last)
{
if (first < last)
{
int mid = first + (last - first) / 2;
MergeSort(arr, first, mid);
MergeSort(arr, mid + 1, last);
merge(arr, first, mid, last);
}
}
void display(char arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout<<"\n"<<arr[i];
}
}
int main()
{
int len = 0;
string file_line;
char arr_names[50];
ifstream file(R"(C:\Users\dell\Desktop\names.txt)");
while (getline(file, file_line))
{
arr_names[len] = file_line.back();
len++;
}
MergeSort(arr_names, 0, len - 1);
cout << "\n Sorted Array of Characters = ";
display(arr_names, len);
}

OUTPUT –
Names.txt file content –

You might also like