Assignment 1
Assignment 1
122CS0075
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:
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