Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

NAME = Naman Jain

BRANCH = Computer Science Engineering (CS2)


ENROLLMENT NUMBER = 0818CS201103
SUBJECT= Analysis Design of Algorithm
S.No. Name of experiment Pg.No. Date of Date of Remark
performance submission
1 Write a program to 1-2
implement Binary Search
Algorithm in Iterative
method.

2 Write a program to 2-4


implement Binary Search
Algorithm in recursive
method.

3 Write a program to 4-6


implement Merge Sort
algorithm.

4 Write a program to 6-9


implement Quick Sort
Algorithm.

5 Write a program to 9 - 11
implement Strassen’s
Matrix Multiplication.

6 Write a program to 12 - 13
implement Optimal
Merge Pattern.
INDORE INSTITUTE OF SCIENCE AND TECHNOLOGY
COMPUTER SCIENCE AND ENGINEERING DEPARTMENT

EXPERIMENT 1 :

Write a program to implement Binary Search Algorithm in Iterative method.

Source Code :

#include<iostream> using

namespace std;

int main(){

int n, flag=0; cout<<"Enter

the size: "<<endl;

cin>>n;

cout<<"Enter elements: "<<endl;

int arr[n]; for(int i=0; i<n; i++){

cin>>arr[i];

int value, l=0;

int h=n-1;

cout<<"Enter the value to find: "<<endl;

cin>>value;

while(l<=h){ int

mid=(l+h)/2;

if(arr[mid]==value){

flag=1;

cout<<"Element found at "<<mid<<" index"<<endl;

break;

else if(value>arr[mid]){

l=mid+1;

else{ h=

mid-1;

Naman Jain Page. 1


0818CS201103
INDORE INSTITUTE OF SCIENCE AND TECHNOLOGY
COMPUTER SCIENCE AND ENGINEERING DEPARTMENT
}

if(flag==0){

cout<<"Element not found!"<<endl;

return 0;

Output:

EXPERIMENT 2 :

Write a program to implement Binary Search Algorithm in recursive method.

Source code :

#include<iostream>

using namespace std; int

f=0;

void binarysearch(int arr[], int l, int h, int value){

Naman Jain Page.2


0818CS201103
INDORE INSTITUTE OF SCIENCE AND TECHNOLOGY
COMPUTER SCIENCE AND ENGINEERING DEPARTMENT

if(l>h){

if(f==0){

cout<<"Element not found!"<<endl;

return;

int mid=(l+h)/2;

if(arr[mid]==value){

f=1;

cout<<"Value found at "<<mid<<" index"<<endl;

return;

if(value>arr[mid]){

binarysearch(arr, mid+1, h, value);

else{

binarysearch(arr, l, mid-1, value);

} int

main(){

int n;

cout<<"Enter the size: "<<endl; cin>>n;

cout<<"Enter elements:

"<<endl; int arr[n]; for(int i=0;

i<n; i++){ cin>>arr[i];

int value;

cout<<"Enter the value to find: "<<endl;

cin>>value;

binarysearch(arr, 0, n-1, value);

Naman Jain Page.3


0818CS201103
INDORE INSTITUTE OF SCIENCE AND TECHNOLOGY
COMPUTER SCIENCE AND ENGINEERING DEPARTMENT

return 0;

Output:

EXPERIMENT 3:

Write a program to implement Merge Sort algorithm.

Source Code:

#include<iostream> using

namespace std;

void merge(int arr[], int l, int mid, int h){

int i, j, k;

i=l;

j=mid+1; k=l;

int temp[100];

while(i<=mid && j<=h){

if(arr[i]<arr[j]){

temp[k]=arr[i];

i++; k++;

else{

temp[k]=arr[j];

j++; k++;

Naman Jain Page.4


0818CS201103
INDORE INSTITUTE OF SCIENCE AND TECHNOLOGY
COMPUTER SCIENCE AND ENGINEERING DEPARTMENT

while(i<=mid){

temp[k]=arr[i];

i++;

k++;

while(j<=h){

temp[k]=arr[j];

j++;

k++;

} for(int i=0; i<=h;

i++){

arr[i]=temp[i]

void mergesort(int arr[], int l, int h){

if(l<h){ int mid=(l+h)/2;

mergesort(arr, l, mid);

mergesort(arr, mid+1, h);

merge(arr, l, mid, h);

} void print(int arr[], int

n){

for(int i=0; i<n; i++){

cout<<arr[i]<<" ";

cout<<endl;

} int

main(){

Naman Jain Page.5


0818CS201103
INDORE INSTITUTE OF SCIENCE AND TECHNOLOGY
COMPUTER SCIENCE AND ENGINEERING DEPARTMENT
int n;

cout<<"Enter the size for the array: "<<endl;

cin>>n;

int arr[n]; cout<<"Enter

elements: "<<endl; for(int i=0;

i<n; i++){

cin>>arr[i];

cout<<"Before sorting: "<<endl;

print(arr, n); mergesort(arr, 0, n-1);

cout<<"After sorting: "<<endl;

print(arr, n);

return 0;

Output:

EXPERIMENT 4:

Write a program to implement Quick Sort Algorithm.

Naman Jain Page.6


0818CS201103
INDORE INSTITUTE OF SCIENCE AND TECHNOLOGY
COMPUTER SCIENCE AND ENGINEERING DEPARTMENT

Sourcecode:

#include<iostream> using

namespace std;

int partition(int arr[], int l, int h){

int

pivot=arr[l]; int

i=l+1; int j=h; int

temp;

do{ while(arr[i]<=pi

vot){

i++;

while(arr[j]>pivot){

j--;

if(i<j){

temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

while(i<j);

temp=arr[l];

arr[l]=arr[j];

arr[j]=temp; return j;

void quicksort(int arr[], int l, int h){

int pi;

if(l<h){

pi=partition(arr, l, h);

quicksort(arr,

Naman Jain Page.7


0818CS201103
INDORE INSTITUTE OF SCIENCE AND TECHNOLOGY
COMPUTER SCIENCE AND ENGINEERING DEPARTMENT
l, pi-1);

quicksort(arr, pi+1, h);

} void print(int arr[], int

n){

for(int i=0; i<n; i++){

cout<<arr[i]<<" ";

cout<<endl;

} int

main(){

int n;

cout<<"Enter the size for the array: "<<endl;

cin>>n;

int arr[n]; cout<<"Enter

elements: "<<endl; for(int i=0;

i<n; i++){

cin>>arr[i];

cout<<"Before Sorting: "<<endl;

print(arr, n); quicksort(arr, 0, n-1);

cout<<"After Sorting: "<<endl;

print(arr, n);

return 0;

Naman Jain Page.8


0818CS20110
INDORE INSTITUTE OF SCIENCE AND TECHNOLOGY
COMPUTER SCIENCE AND ENGINEERING DEPARTMENT
Output:

EXPERIMENT 5:

Write a program to implement Strassen’s Matrix Multiplication.

Sourcecode: #include

<iostream> using

namespace std;

void multiply(int[5][5], int[5][5], int, int, int);

int display(int[5][5], int, int); int main()

int a[5][5], b[5][5], r1, c1, r2, c2;

cout<<"\n Enter rows for first matrix: ";

cin>>r1;

cout<<"\n Enter columns for second matrix: ";

cin>>c1;

cout<<"\n Enter rows for first matrix: ";

cin>>r2;

cout<<"\n Enter columns for second matrix: ";

cin>>c2;

Naman Jain Page.9


0818CS20110
INDORE INSTITUTE OF SCIENCE AND TECHNOLOGY
COMPUTER SCIENCE AND ENGINEERING DEPARTMENT
if (c1 != r2)
return 0;

cout<<"\n Enter elements of first matrix \n";

for(int i=0; i<r1; i++)

for(int j=0; j<c1; j++)

cin>>a[i][j];

cout<<"\n Enter elements of second matrix\n";

for(int i=0; i<r2; i++)

for(int j=0; j<c2; j++)

cin>>b[i][j];

} display(a,r1,c1);

display(b,r2,c2);

multiply(a, b, r1, c2, c1);

return 0;

void multiply(int a[5][5], int b[5][5], int row, int col, int c1)

{ int c[5][5]; for(int

i=0; i<row; i++)

for(int j=0; j<col; j++)

c[i][j]=0;

for(int i=0; i<row; i++)

for(int j=0; j<col; j++)

Naman Jain page.10


0818CS201103
INDORE INSTITUTE OF SCIENCE AND TECHNOLOGY
COMPUTER SCIENCE AND ENGINEERING DEPARTMENT
for(int k=0; k<c1; k++)//columns of first matrix || rows of second matrix

c[i][j]+=a[i][k]*b[k][j];

cout<<"\n Matrix c after matrix multiplication is:\n";

display(c, row, col);

int display(int c[5][5], int row, int col)

cout<<"\n Matrix is:\n";

for(int i=0; i<row; i++) {

for(int j=0; j<col; j++)

cout<<c[i][j]<<" "; cout<<"\n";

return 0;

Output:

Naman Jain page.11


0818CS201103
INDORE INSTITUTE OF SCIENCE AND TECHNOLOGY
COMPUTER SCIENCE AND ENGINEERING DEPARTMENT

EXPERIMENT 6:

Write a program to implement Optimal Merge Pattern.

Sourcecode: #include <iostream>

using namespace std ; int main() {

int i,k,a[10],c[10],n,l; cout<<"Enter

the no. of elements\t"; cin>>n;

cout<<"\nEnter the sorted elments for optimal merge pattern";

for(i=0;i<n;i++) { cout<<"\t"; cin>>a[i]; } i=0;k=0; c[k]=a[i]

+a[i+1]; i=2; while(i<n) { k++;

if((c[k-1]+a[i])<=(a[i]+a[i+1]))

{ c[k]=c[k-

1]+a[i]; } else {

c[k]=a[i]+a[i+1];

i=i+2;

while(i<n)

{ k+

+;

if((c[k-1]+a[i])<=(c[k-2]+a[i]))

{ c[k]=c[k-1]+a[i];

} else { c[k]=c[k-

2]+a[i]; } i++; } }

i++; } k++;

c[k]=c[k-1]+c[k-

2];

cout<<"\n\nThe optimal sum are as follows......\n\n";

for(k=0;k<n-1;k++) { cout<<c[k]<<"\t"; } l=0;

for(k=0;k<n-1;k++) { l=l+c[k];

}cout<<"\n\n The external path length is......."<<l;

Output:

Naman Jain page.12


0818CS201103
INDORE INSTITUTE OF SCIENCE AND TECHNOLOGY
COMPUTER SCIENCE AND ENGINEERING DEPARTMENT

Naman Jain page.13


0818CS201103

You might also like