0% found this document useful (0 votes)
43 views9 pages

Assignment2 ADA PDF

The document contains the code and output for 4 questions from a lab assignment on algorithms. Question 1 implements quicksort to sort an array and analyzes its time complexity. Question 2 implements Strassen's matrix multiplication algorithm to optimize matrix multiplication. Question 3 implements merge sort to sort an array. Question 4 finds the median of two merged sorted arrays in O(log n) time using a binary search approach.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views9 pages

Assignment2 ADA PDF

The document contains the code and output for 4 questions from a lab assignment on algorithms. Question 1 implements quicksort to sort an array and analyzes its time complexity. Question 2 implements Strassen's matrix multiplication algorithm to optimize matrix multiplication. Question 3 implements merge sort to sort an array. Question 4 finds the median of two merged sorted arrays in O(log n) time using a binary search approach.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

LAB ASSIGNMENT 2

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;

// Partition function to divide the array into two subarrays


int partition(int arr[], int low, int high){
int pivot = arr[low];
int i = low + 1;
for(int j = low + 1; j <= high; j++){
if(arr[j] < pivot){
swap(arr[i], arr[j]);
i++;
}
}
swap(arr[low], arr[i-1]);
return i-1;
}

// QuickSort function to recursively sort the array


void quickSort(int arr[], int low, int high){
if(low < high){
int pi = partition(arr, low, high);
quickSort(arr, low, pi-1);
quickSort(arr, pi+1, high);
}
}

// Function to print the array


void printArray(int arr[], int size){
for(int i=0; i < size; i++){
cout << arr[i] << " ";
}
cout << endl;
}

int main(){
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);

cout << "Input array: ";


printArray(arr, n);

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;

// Function to add two matrices


void addMatrix(int A[][2], int B[][2], int C[][2]){
for(int i=0; i<2; i++){
for(int j=0; j<2; j++){
C[i][j] = A[i][j] + B[i][j];
}
}
}

// Function to subtract two matrices


void subtractMatrix(int A[][2], int B[][2], int C[][2]){
for(int i=0; i<2; i++){
for(int j=0; j<2; j++){
C[i][j] = A[i][j] - B[i][j];
}
}
}

// Function to multiply two matrices


void multiplyMatrix(int A[][2], int B[][2], int C[][2]){
for(int i=0; i<2; i++){
for(int j=0; j<2; j++){
C[i][j] = 0;
for(int k=0; k<2; k++){
C[i][j] += A[i][k] * B[k][j];
}
}
}
}

// Function to divide a matrix into four submatrices


void divideMatrix(int A[][2], int A11[][2], int A12[][2], int A21[][2], int
A22[][2], int n){
for(int i=0; i<n/2; i++){
for(int j=0; j<n/2; j++){
A11[i][j] = A[i][j];
A12[i][j] = A[i][j+n/2];
A21[i][j] = A[i+n/2][j];
A22[i][j] = A[i+n/2][j+n/2];
}
}
}

// Function to merge four submatrices into a single matrix


void mergeMatrix(int C[][2], int C11[][2], int C12[][2], int C21[][2], int
C22[][2], int n){
for(int i=0; i<n/2; i++){
for(int j=0; j<n/2; j++){
C[i][j] = C11[i][j];
C[i][j+n/2] = C12[i][j];
C[i+n/2][j] = C21[i][j];
C[i+n/2][j+n/2] = C22[i][j];
}
}
}

// Function to perform Strassen's matrix multiplication algorithm


void strassenMatrixMultiply(int A[][2], int B[][2], int C[][2], int n){
if(n == 1){
C[0][0] = A[0][0] * B[0][0];
return;
}

int A11[n/2][2], A12[n/2][2], A21[n/2][2], A22[n/2][2];


int B11[n/2][2], B12[n/2][2], B21[n/2][2], B22[n/2][2];
int C11[n/2][2], C12[n/2][2], C21[n/2][2], C22[n/2][2];
int M1[n/2][2], M2[n/2][2], M3[n/2][2], M4[n/2][2], M5[n/2][2], M6[n/2][2],
M7[n/2][2];
int P1[n/2][2], P2[n/2][2], P3[n/2][2], P4[n/2][2];
int temp1[n/2][2], temp2[n/2][2];

// Divide matrices A and B into four submatrices


divideMatrix(A, A11, A12, A21, A22, n);
divideMatrix(B, B11, B12, B21, B22, n);

// Compute seven products of submatrices


subtractMatrix(B12, B22, temp1);
strassenMatrixMultiply(A11, temp1, M1, n/2);
addMatrix(A11, A12, temp1);
strassenMatrixMultiply(temp1, B22, M2, n/2);

addMatrix(A21, A22, temp1);


strassenMatrixMultiply(temp1, B11, M3, n/2);

subtractMatrix(B21, B11, temp1);


strassenMatrixMultiply(A22, temp1, M4, n/2);

addMatrix(A11, A22, temp1);


addMatrix(B11, B22, temp2);
strassenMatrixMultiply(temp1, temp2, M5, n/2);

subtractMatrix(A12, A22, temp1);


addMatrix(B21, B22, temp2);
strassenMatrixMultiply(temp1, temp2, M6, n/2);

subtractMatrix(A11, A21, temp1);


addMatrix(B11, B12, temp2);
strassenMatrixMultiply(temp1, temp2, M7, n/2);

// Compute four subproducts


addMatrix(M5, M4, temp1);
subtractMatrix(temp1, M2, temp2);
addMatrix(temp2, M6, P1);

addMatrix(M1, M2, P2);

addMatrix(M3, M4, P3);

addMatrix(M5, M1, temp1);


subtractMatrix(temp1, M3, temp2);
subtractMatrix(temp2, M7, P4);

// Merge four subproducts into a single matrix


mergeMatrix(C, P1, P2, P3, P4, n);
}
// Function to print a matrix
void printMatrix(int A[][2], int n){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
cout << A[i][j] << " ";
}
cout << endl;
}
}

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

4. Given two sorted arrays with size n each, write an algorithm to


find the median of the array Obtained after merging the above 2
arrays. (The complexity should be O(log(n)), you may use
auxiliary space)

#include <iostream>
using namespace std;

double findMedian(int arr1[], int arr2[], int n) {


int low = 0, high = n - 1;

while (low <= high) {


int mid = (low + high) / 2;
int secondMid = n - mid - 1;

if (arr1[mid] <= arr2[secondMid]) {


if (mid == n - 1 || arr2[secondMid] <= arr1[mid + 1]) {
double leftMax = (mid == 0) ? arr2[secondMid] : max(arr1[mid],
arr2[secondMid-1]);
double rightMin = (mid == n - 1) ? arr1[mid] : min(arr1[mid+1],
arr2[secondMid]);
return (leftMax + rightMin) / 2.0;
}
else
low = mid + 1;
}
else {
if (secondMid == n - 1 || arr1[mid] <= arr2[secondMid + 1]) {
double leftMax = (secondMid == 0) ? arr1[mid] : max(arr1[mid-1],
arr2[secondMid]);
double rightMin = (secondMid == n - 1) ? arr2[secondMid] :
min(arr1[mid], arr2[secondMid+1]);
return (leftMax + rightMin) / 2.0;
}
else
high = mid - 1;
}
}
return -1;
}

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]);

double median = findMedian(arr1, arr2, n);

cout << "Median of the merged array is: " << median;

return 0;
}

Output

You might also like