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

IT265– Design & Analysis of Algorithm 4IT (2023-24)

Practical 2
Aim: Implement and analyze the algorithm of Bubble Sort.

Program:

#include<iostream>
using namespace std;
int s=0,comp=0,c=0;
int main()
{
int temp;
// int a[5] = {45, 1, 32, 13, 26};
//int a[] ={5,4,3,2,1},ex=0;
int a[]={3,2,1,4,5},ex=0;
// int a[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25},ex=0;
int n = sizeof(a)/sizeof(a[0]);
c++;//i=0;
for(int i = 0; i < n-1; i++)
{
c++;//i
c++;//j=i+1
ex=0;
c++;//ex=0
for(int j = 0; j < n-i-1; j++)
{
c++;//j<n
c++;//if condition
comp++;//comparision
if(a[j] > a[j+1])
{

ex++;
c++;//ex++;
temp = a[j+1];
c++;// temp = a[i];
a[j+1] = a[j];
c++;// a[i] = a[j];
a[j] = temp;
c++;// a[j] = temp;
s++;//exchange
}
c++;//j++
}
c++;//false condition for loop j
c++;//if condition
if(ex==0)

ID No. 22IT109 Page 1


IT265– Design & Analysis of Algorithm 4IT (2023-24)

{
c++;//break
break;
}

c++;//i++
}
cout<<"Array printing :";
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
c++;//false condition for loop i
c++;//return
cout<<"Operation "<<c<<endl;
cout<<"Comparison "<<comp<<endl;
cout<<"Exchange "<<s<<endl;
return 0;
}

Output:

ID No. 22IT109 Page 2


IT265– Design & Analysis of Algorithm 4IT (2023-24)

Analysis Table:

Best case Average case Worst


case

Insert Operation Comparison Exchange Operation Comparison Exchange Operation Comparison Exchange

5 21 4 0 60 9 3 97 10 10

10 36 9 0 178 35 10 372 45 45

15 51 14 0 415 84 28 822 105 105

20 66 19 0 678 145 45 1447 190 190

25 81 24 0 1095 234 78 2247 300 300

Graph:

BEST CASE:

AVERAGE CASE:

ID No. 22IT109 Page 3


IT265– Design & Analysis of Algorithm 4IT (2023-24)

WORST CASE:

Conclusion: In conclusion, Bubble Sort, though simple to implement, exhibits an O(n^2) worst-
case time complexity, rendering it inefficient for large datasets. Its practical drawbacks make it
less suitable for real-world applications.

2)Aim: Implement and analyze the algorithm of Selection Sort.

Program:

#include <bits/stdc++.h>
using namespace std;

ID No. 22IT109 Page 4


IT265– Design & Analysis of Algorithm 4IT (2023-24)

int s=0,comp=0,c=0;
int main()
{
//int arr[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
//int arr[] ={25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
int arr[] = {13,12,11,10,9,8,7,6,5,4,3,2,1,14,15,16,17,18,19,20,21,22,23,24,25};
int n = sizeof(arr) / sizeof(arr[0]);
int i, j, min_idx;
c++;//i=0
for (i = 0; i < n - 1; i++)
{
c++;//i<n-1
min_idx = i;
c++;//min_idx=i;

c++;//j=i+1
for (j = i + 1; j < n; j++)
{
c++;//j<n

c++;//if condition
comp++;
if (arr[j] < arr[min_idx])
{

c++;//min_idx = j;
min_idx = j;
}
c++;//j++
}
c++;//false condition for for loop
c++;//if condition
if (min_idx != i)
{
int temp = arr[min_idx];
c++;//temp = arr[min_idx]
arr[min_idx]=arr[i];
c++;//arr[min_idx]=arr[i];
arr[i]=temp;
c++;// arr[i]=temp;
s++;
}
c++;//i++

}
c++;//false condition

ID No. 22IT109 Page 5


IT265– Design & Analysis of Algorithm 4IT (2023-24)

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


cout << arr[i] << " ";
}
cout << endl;
cout<<"Operation "<<c<<endl;
cout<<"Comparison "<<comp<<endl;
cout<<"Exchange "<<s<<endl;
return 0;
}

Output:

Analysis Table:

ID No. 22IT109 Page 6


IT265– Design & Analysis of Algorithm 4IT (2023-24)

Best case Average case Worst


case

Insert Operation Comparison Exchange Operation Comparison Exchange Operation Comparison Exchange

5 56 10 0 61 10 1 68 10 2

10 191 45 0 203 45 2 231 45 5

15 401 105 0 429 105 4 478 105 7

20 686 190 0 726 190 5 816 190 10

25 1046 300 0 1106 300 6 1238 300 12

Graph:

BEST CASE:

AVERAGE CASE:

ID No. 22IT109 Page 7


IT265– Design & Analysis of Algorithm 4IT (2023-24)

WORST CASE:

Conclusion: In Selection sort best , average , worst all case has O(n^2) time complexity.

ID No. 22IT109 Page 8

You might also like