You are on page 1of 20

PRACTICAL FILE

OF

Algorithms Design and Analysis Lab

(Paper code: ETCS-351)

Submitted to: Submitted by:


Ms. Sanjana Priyanshu Mishra
Assistant Professor 09515603120
IT Dept. Section: T7
Course: B.Tech-IT

DR. AKHILESH DAS GUPTA INSTITUTE OF TECHNOLOGY AND MANAGEMENT

NEW DELHI
INDEX

S.No Experiment Date Remarks T.Sign


1. Write a program to implement Insertion
sort using array as a data structure and
analyse it’s time complexity.

2. Write a program to implement Bubble


sort using array as a data structure and
analyse it’s time complexity.
3. Write a program to implement Selection
sort using array as a data structure and
analyse it’s time complexity.

4. Write a program to implement Quick


sort using array as a data structure and
analyse it’s time complexity.

5. Write a program to implement Merge


sort using array as a data structure and
analyse it’s time complexity.

6. Write a program to implement Linear


Search and analyse it’s time complexity.

7. Write a program to implement Binary


search and analyse it’s time complexity.

8. Write a program to implement Matrix


chain multiplication and analyse it’s time
complexity.
PROGRAM NO: 01
Program Objective: Write a program to implement Insertion sort using array as a
data structure and analyse it’s time complexity.

Program in C++:

#include <iostream>
using namespace std.
int main()
{
int n;
cout<<"Enter Number of Elements
cin>>n;
int num[n];
for(int k=0;k<n;k++)
{
cout<<"Enter Number "<<k+1<<" : ";
cin>>num[k];
}
int i,j,x;
cout<<"Array before Insertion Sort"<<endl;
for(i=0; i<n; i++)
{
cout<<num[i]<<" ";
}
for(i=1; i<n; i++)
{
x=num[i];
j=i-1;
while(j>=0)
{
if(x<num[j])
{
num[j+1]=num[j];
}
else {
break;
}
j=j-1;
}
num[j+1]=x;
}
cout<<"\nArray after Insertion Sort\n”;
for(i=0; i<n; i++)
{
cout<<num[i]<<" ";
}
return 0;
}

Output:

Time Complexity:
A. Best Case: When Array is already sorted ->
Number of Comparisons: n-1
• n: number of Elements
Therefore, Best Case Time Complexity =O(n)
B. Worst Case: Array order is in descending order ->
Number of Comparisons: n(n-1)/2
• n: number of Elements
Therefore, Worst Case Time Complexity =O(n2)
C. Average Case: Time complexity = O(n2)
PROGRAM NO: 02
Program Objective: Write a program to implement Bubble sort using array as a data
structure and analyze it’s time complexity
Program in C++:
#include<iostream>
using namespace std;
int main()
{
cout<<"\nSorting the Array using Bubble Sort Method !!!\n";
int n, i, arr[100], j, temp;
cout<<"Enter the Size of Array (max. 100): ";
cin>>n;
cout<<"Enter "<<n<<" Numbers
for(i=0; i<n; i++)
cin>>arr[i];
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
cout<<"\nArray Sorted Successfully !!!\n”;
cout<<"\nThe New Array is: \n";
for(i=0; i<n; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
Output:

Time Complexity:

A. Best Case: When Array is already sorted ->


Number of Comparisons: (n-1)
• n: number of Elements
Therefore, Best Case Time Complexity =O(n)
B. Worst Case: Array order is in descending order ->
Number of Comparisons: (n-1)(n-1)
• n: number of Elements
Therefore, Worst Case Time Complexity =O(n2)
C. Average Case: Time complexity = O(n2)
PROGRAM NO: 03
Program Objective: Write a program to implement Selection sort using array as a
data structure and analyze it’s time complexity

Program in C++:

#include <iostream>
using namespace std;
int main() {
int arr[10]={6,12,0,18,11,99,55,45,34,2};
int n,i, j, position, swap;
cout<<"Enter the Size of Array (max. 100): ";
cin>>n;
cout<<"Enter "<<n<<" Numbers: ";
for(i=0; i<n; i++)
cin>>arr[i];
for (i = 0; i < (n - 1); i++) {
position = i;
for (j = i + 1; j < n; j++) {
if (arr[position] > arr[j])
position = j;
}
if (position != i) {
swap = arr[i];
arr[i] = arr[position];
arr[position] = swap;
}
}
cout<<"Sorted Array is : ";
for (i = 0; i < n; i++)
cout<<arr[i]<<" ";
return 0;
}
Output:

Time Complexity:

A. Best Case: When Array is already sorted ->


Number of Comparisons: n*(n+1)/2
• n: number of Elements
Therefore, Best Case Time Complexity =O(n2)
B. Worst Case: Case when the array is already sorted (with one
swap) but the smallest element is the last element. -> Number of
Comparisons: n*(n+1)/2
• n: number of Elements
Therefore, Worst Case Time Complexity =O(n2)
C. Average Case: Time complexity = O(n2)
PROGRAM NO: 04
Program Objective: Write a program to implement Quick sort using array as a data
structure and Analyze it’s time complexity

Program in C++:

#include<iostream>
using namespace std;
void swap(int arr[] , int pos1, int pos2){
int temp;
temp = arr[pos1];
arr[pos1] = arr[pos2];
arr[pos2] = temp;
}
int partition(int arr[], int low, int high, int pivot){
int i = low;
int j = low;
while( i <= high){
if(arr[i] > pivot){
i++;
}
else{
swap(arr,i,j);
i++;
j++;
}
}
return j-1;
}
void quickSort(int arr[], int low, int high){
if(low < high){
int pivot = arr[high];
int pos = partition(arr, low, high,
pivot);

quickSort(arr, low, pos-1);


quickSort(arr, pos+1, high);
}
}
int main()
{
int n ;
cout <<"Enter the Number Of Elements : ";
cin>>n;
int arr[n];
for( int i = 0 ; i < n; i++){
cout<<"Enter Number "<<i+1<<" : ";
cin>> arr[i];
}
cout<<"Sorting the Array using Quick Sort Method !!!\n";
cout<<"The Unsorted array is: \n";
for( int i = 0 ; i < n; i++){
cout<< arr[i]<<" ";
}
quickSort(arr, 0 , n-1);
cout<<"\nThe Sorted array is: \n";
for( int i = 0 ; i < n; i++){
cout<< arr[i]<<" ";
}
}
Output:

Time Complexity:
A. Best Case: the best case of quick sort is when we will select pivot as a mean element.
Number of Operations: logn*n
• n: number of Elements
Therefore, Best Case Time Complexity =O(nlogn)
B. Worst Case: when our array will be sorted and we select smallest or largest indexed
element as pivot. ->
Number of Operations: n*(n+1)/2
• n: number of Elements
Therefore, Worst Case Time Complexity =O(n2) 3.
C. Average Case: Time complexity = O(n2)
PROGRAM NO: 05
Program Objective: Write a program to implement Merge sort using array as a data
structure and analyze it’s time complexity.

Program in C++:
#include <iostream>
using namespace std;
void merge(int arr[], int p, int q, int r)
{
int n1 = q - p + 1;
int n2 = r - q;
int L[n1], M[n2];
for (int i = 0; i < n1; i++)
L[i] = arr[p + i];
for (int j = 0; j < n2; j++)
M[j] = arr[q + 1 + j];
int i, j, k;
i = 0;
j = 0;
k = p;
while (i < n1 && j < n2)
{
if (L[i] <= M[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = M[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = M[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
int main()
{
int n ;
cout <<"Enter the Number Of Elements : ";
cin>>n;
int arr[n];
for( int i = 0 ; i < n; i++){
cout<<"Enter Number "<<i+1<<" : ";
cin>> arr[i];
}
cout<<"Sorting the Array using Merge Sort Method !!!\n";
cout<<"The Unsorted array is: \n";
for( int i = 0 ; i < n; i++){
cout<< arr[i]<<" ";
}
mergeSort(arr, 0, n - 1);
cout<<"\nThe Sorted array is: \n";
for( int i = 0 ; i < n; i++){
cout<< arr[i]<<" ";
}
return 0;
}
Output:

Time Complexity:

A. Best Case: One can assume that the array is already sorted so in that case the
number of comparisons would be minimum. Number of Comparisons: n*logn
• n: number of Elements
Therefore, Best Case Time Complexity =O(nlogn)
B. Worst Case: first, divide the array into smaller parts then do comparisons on the
smaller parts and rearrange them and then perform a merging so one can say the
order which leads to the maximum number of comparisons will give us the worst
time complexity.
Number of Comparisons: n*logn
• n: number of Elements
Therefore, Worst Case Time Complexity =O(n*logn)
C. Average Case: Time complexity = O(nlogn)
PROGRAM NO: 06
Program Objective: Write a program to implement Linear Search and analyze it’s time
complexity.

Program in C++:

#include <iostream>
using namespace std;
int search(int arr[], int N, int x)
{
int i;
for (i = 0; i < N; i++)
if (arr[i] == x)
return i;
return -1;
}
int main()
{
cout<<"\nSearching The Element Using Linear Search Method !!!\n";
int arr[] = {2, 4, 0, 1, 9};
cout<<"Enter the Number You Want To Search (0-9) : ";
int x;
cin>>x;
int N = sizeof(arr) / sizeof(arr[0]);
int result = search(arr, N, x);
if (result == -1){
cout << "-> Sorry !! Element is not present in the Array";
}
else{
cout << "-> Element is present at index " << result;
}
return 0;
}
Output:

Time Complexity:

A. Best Case: The element to be search is on the first index.


Number of Comparisons: 1
Therefore, Best Case Time Complexity =O(1)
B. Worst Case: The element to be search is in the last index and the element to be search
is not present in the list
Number of Comparisons: n
• n: number of Elements
Therefore, Worst Case Time Complexity =O(n)
C. Average Case: Time complexity = O(n)
PROGRAM NO: 07
Program Objective: Write a program to implement Binary Search and analyse it’s time
complexity.

Program in C++:

#include <iostream>
using namespace std;
int binarySearch(int array[], int x, int low, int high) { while
(low <= high) {

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

if (array[mid] == x){
return mid;
}
if (array[mid] < x){
low = mid + 1;
}
else{
high = mid - 1;
}
}
return -1;
}
int main()
{
cout<<"\nSearching The Element Using Binary Search Method !!!\n";
int array[] = {3, 4, 5, 6, 7, 8, 9};
cout<<"Enter the Number You Want To Search (0-9) : ";
int x;
cin>>x;
int n = sizeof(array) / sizeof(array[0]);
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
cout<<"-> Sorry !! Element is not present in the Array";
else {
cout<<"Element is found at index "<< result;
}
Output:

Time Complexity:
A. Best Case: The element to be search is in the middle of the list
Number of Comparisons: 1
Therefore, Best Case Time Complexity =O(1)
B. Worst Case: The element is to search is in the first index or last index
Number of Comparisons: logn
• n: number of Elements
Therefore, Worst Case Time Complexity =O(logn)
C. Average Case: Time complexity = O(logn)
PROGRAM NO: 08
Program Objective: Write a program to implement Binary Search and analyze it’s time
complexity.

Program in C++:

#include<bits/stdc++.h>
using namespace std;
int MatrixChainOrder(int p[], int n)
{
int m[n][n];
int i, j, k, L, q;

for (i=1; i<n; i++)


m[i][i] = 0;
for (L=2; L<n; L++) //filling half table only
{
for (i=1; i<n-L+1; i++)
{ j = i+L-1;
m[i][j] = INT_MAX;
for (k=i; k<=j-1; k++)
{
q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];
if (q < m[i][j])
m[i][j] = q;
}
}
}
return m[1][n-1];
}
int main()
{
int n;
cout<<"\n!!! Performing The Matrix Chain Multiplication !!!\n";
cout<<"Enter Number Of Arrays For Matrix Chain Multiplication : ";
cin>>n;
int arr[n+1];
for(int i=0;i<=n;i++){
if(i==n){
cout<<"Enter Number Of Columns In "<<n<<" Matrix : ";
cin>>arr[n];
} else{
cout<<"Enter Number Of Rows In "<<i+1<<" Matrix : ";
cin>>arr[i];
}
}
int size = n+1;
cout<<"Minimum Number Of Multiplication Of "<<n<<" Matrices Will Be
"<<MatrixChainOrder(arr,size)<<" Multiplications !!!";
}

Output:

Time Complexity:
A. Best Case: One can assume that the array is already sorted so in that case the
number of comparisons would be minimum. Number of Comparisons: n*logn
• n: number of Elements
Therefore, Best Case Time Complexity =O(n3) as there could be O(n^2) unique subproblems
to any MCM given problem and for every such sub-problem there could be O(n) splits
possible. So, it is O(n^3).
B. Worst Case: The columns of matrix A is not equal to rows of matrix B.
Number of Operations: 0
Therefore, Worst Case Time Complexity =O(0)
C. Average Case: Time complexity = O(0).

You might also like