Proper Steps for Bubble Sort Algorithm

Status
Not open for further replies.

Hector3436

Honorable
Sep 5, 2012
5
0
10,510
function = bubblesort(X)
% Bubble sort implementation
len = length(X);
S=X;
% t0=tic;
sorted = false;
while (~sorted)
sorted = true;
for i=1:len-1
if S(i) > S(i+1)
tmp = S(i);
S(i) = S(i+1);
S(i+1) = tmp;
sorted = false;
end
end
end
0% t1=toc;
% runtime = t0-t1;


Good evening everyone. i am doing a algorithm analysis of this simple bubble sort and I am looking for the proper way to do the probability of the if function occurring in its total n loops. Any suggestions are welcome. Thank you.
 

Scott_D_Bowen

Honorable
Nov 28, 2012
50
0
10,590
Calculate the probability of if S(i) > S(i+1) and then multiply it by the number of iterations?

I am guessing this is some weird *** University "algorithm analysis" class, and your professor will want a MUCH better answer than what I just gave. (or perhaps not, maybe he/she just wants a simple explanation).

I do however expect that he/she would want an answer in a certain format.
 
Status
Not open for further replies.