Assignment2 ADA PDF
Assignment2 ADA PDF
ANALYSIS AND
DESIGN OF
ALGORITHMS
(CS1401)
NAME-MISHANK KUMAR
SID -21103107
Q1. Implement the partition() and quickSort() functions to sort an
array. Analyze time complexity and the underlying recurrence
relation.
#include<iostream>
using namespace std;
int main(){
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n-1);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}
Output
Q2. You are given the task to optimize a module for a digital
calculator that can compute the product of two coefficient matrices
of order 2n, where n is a natural number. Implement Strassen’s
matrix multiplication algorithm for faster calculation of the product.
#include<iostream>
using namespace std;
int main(){
cout<<"NAME-MISHANK KUMAR";
cout<<endl;
cout<<"SID-21103107"<<endl;
int A[][2] = {{1, 2}, {3, 4}};
int B[][2] = {{5, 6}, {7, 8}};
int C[2][2];
strassenMatrixMultiply(A, B, C, 4);
printMatrix(C, 4);
return 0;
}
Output
3. Consider an array, “A: 27, 17, 02, 13, 19, 07, 18”. Write a program
to sort this array using merge sort algorithm.
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
void Merge(int arr[],int s,int e){
int mid=s+(e-s)/2;
int len1=mid-s+1;
int len2=e-mid;
int *arr1=new int[len1];
int *arr2=new int[len2];
int k=s;
for(int i=0;i<len1;i++){
arr1[i]=arr[k++];
}
k=mid+1;
for(int i=0;i<len2;i++){
arr2[i]=arr[k++];
}
int index1=0;
int index2=0;
k=s;
while(index1<len1 && index2<len2){
if(arr1[index1]<arr2[index2]){
arr[k++]=arr1[index1++];
}
else{
arr[k++]=arr2[index2++];
}
}
while(index1<len1){
arr[k++]=arr1[index1++];
}
while(index2<len2){
arr[k++]=arr2[index2++];
}
}
void MergeSort(int arr[],int s,int e){
if(s>=e){
return ;
}
int mid=s+(e-s)/2;
MergeSort(arr,s,mid);
MergeSort(arr,mid+1,e);
Merge(arr,s,e);
}
int main(){
cout<<"MISHANK KUMAR"<<endl;
cout<<"SID-21103107"<<endl;
int arr[10]={18,56,27,7,8,4,9,31,81,1};
cout<<"Array before sorting-->"<<endl;
for(auto it:arr){
cout<<it<<" ";
}
cout<<endl;
cout<<"Array after sorting-->"<<endl;
MergeSort(arr,0,9);
for(int i=0;i<10;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}
Output
#include <iostream>
using namespace std;
int main() {
cout<<"NAME-MISHANK KUMAR"<<endl;
cout<<"SID-21103107"<<endl;
int arr1[] = {1, 3, 5, 7, 9};
int arr2[] = {2, 4, 6, 8, 10};
int n = sizeof(arr1) / sizeof(arr1[0]);
cout << "Median of the merged array is: " << median;
return 0;
}
Output