0% found this document useful (0 votes)
2 views

Assignment 1

The document outlines the implementation and analysis of the Bubble Sort algorithm using two variations, bs1 and bs2, to count comparisons during sorting. It also includes a Coin Toss problem simulation to analyze the probability of heads in a series of tosses. The results are visualized through plots comparing the number of elements and comparisons for the sorting algorithms and the probability for the coin toss.

Uploaded by

sai713194
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment 1

The document outlines the implementation and analysis of the Bubble Sort algorithm using two variations, bs1 and bs2, to count comparisons during sorting. It also includes a Coin Toss problem simulation to analyze the probability of heads in a series of tosses. The results are visualized through plots comparing the number of elements and comparisons for the sorting algorithms and the probability for the coin toss.

Uploaded by

sai713194
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 7

Name : Ankur Dutta

122CS0075

Bubble sort Algorithm

k=1;
temp=0;
c=0;
for n=10:10:100
xne(k)=n;
a=round(rand(1,n)*100);
%----------------------
%----------------------
ycb(k)=bs1(a,n);
ycd(k)=bs2(a,n);

c=0;
k=k+1;
end
plot (xne,ycb,xne,ycd);
title('Bubble Sort Algorithm')
xlabel('Number of Elements')
ylabel('Comparisions')
legend()
grid on

#bs1.m

function c = bs1(a,n)
%UNTITLED6 Summary of this function goes here
% Detailed explanation goes here
c=0;
for i = 1:n-1
for j = 1:n-i
c=c+1;
if a(j)>a(j+1)
temp = a(j);
a(j) = a(j+1);
a(j+1) = temp;
end
end
end
end

#bs2.m
function c = bs2(a,n)
%UNTITLED8 Summary of this function goes here
% Detailed explanation goes here
c = 0;
for i=1:n-1
ex=0;
for j=1:n-i
c=c+1;
if a(j)>a(j+1)
ex=1;
temp=a(j);
a(j)=a(j+1);
a(j+1)=temp;
end

end
if ex==0
break;
end
end
end
Conclusions:

Average of the bubble sort

k=1;
temp=0;
c=0;
for n=10:10:100
a1=0;
a2=0;
np=100;
for avg=1:np
a=round(rand(1,n)*100);
b=round(rand(1,n)*100);
a1=a1+bs1(b,n);
a2=a2+bs2(a,n);
end
xne(k)=n;
ycb(k)=a1/np;
ycd(k)=a2/np;

c=0;
k=k+1;
end
plot (xne,ycb,xne,ycd);
title('Average Bubble Sort Algorithm')
xlabel('Number of Elements')
ylabel('Comparisions')
legend()
grid on

ada1.m
function c = bs1(a,n)
%UNTITLED6 Summary of this function goes here
% Detailed explanation goes here
c=0;
for i = 1:n-1
for j = 1:n-i
c=c+1;
if a(j)>a(j+1)
temp = a(j);
a(j) = a(j+1);
a(j+1) = temp;
end
end
end
end

ada2.m
function c = bs2(a,n)
%UNTITLED8 Summary of this function goes here
% Detailed explanation goes here
c = 0;
for i=1:n-1
ex=0;
for j=1:n-i
c=c+1;
if a(j)>a(j+1)
ex=1;
temp=a(j);
a(j)=a(j+1);
a(j+1)=temp;
end

end
if ex==0
break;
end
end
end
COIN TOSS PROBLEM
%==========COINTOSS PROBLEM=============
xne = zeros(1,10);
yne = zeros(1,10);
sm = zeros(1,10);
car = 1;
%create an array of 1000 elements
for i= 100:100:100000
xne(car) = i;
prob = 0;
for j=1:i
prob = prob+randi([0,1]);
end
prob = prob/i;
yne(car) = prob;
sm(car)=0.4;
car = car+1;
end
plot(xne,yne,xne,sm)
title('Coin Toss Problem')
xlabel('x-axis')
ylabel('y-axis')
legend("ANKUR DUTTA","ASSIGNMENT2")
grid on

You might also like