Proper Steps for Bubble Sort Algorithm

Status
Not open for further replies.

Hector3436

Honorable
Sep 5, 2012
5
0
10,510
0
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
1
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.
Thread starter Similar threads Forum Replies Date
A Apps General Discussion 1
C Apps General Discussion 2
C Apps General Discussion 1
Z Apps General Discussion 10
M Apps General Discussion 3
M Apps General Discussion 4
S Apps General Discussion 2
A Apps General Discussion 1
Z Apps General Discussion 2
Archgaull Apps General Discussion 10
Z Apps General Discussion 1
Marius Hagen Apps General Discussion 2
B Apps General Discussion 1
Y Apps General Discussion 1
K Apps General Discussion 2
L Apps General Discussion 1
J Apps General Discussion 1
I Apps General Discussion 8
F Apps General Discussion 4
gamerxavier Apps General Discussion 6

ASK THE COMMUNITY