# bubble sort analysis and probability

Page 1 of 1

## 6 Replies - 3844 Views - Last Post: 14 December 2012 - 09:08 PM

Reputation: 0
• Posts: 4
• Joined: 13-December 12

# bubble sort analysis and probability

Posted 13 December 2012 - 02:29 PM

Hi, ineed to analize some bubble sort algorithms for a special project, and calculate the probability of each, while, if, for statements. Please help me
Is This A Good Question/Topic? 0

## Replies To: bubble sort analysis and probability

### #2 ipushmycar

• D.I.C Regular

Reputation: 86
• Posts: 390
• Joined: 29-August 10

## Re: bubble sort analysis and probability

Posted 13 December 2012 - 02:31 PM

special project.. homework.

What language is it in?
Have you written the bubblesort yet?

Reputation: 0
• Posts: 4
• Joined: 13-December 12

## Re: bubble sort analysis and probability

Posted 13 December 2012 - 02:33 PM

Its on matlab, ill write them down for u

Reputation: 0
• Posts: 4
• Joined: 13-December 12

## Re: bubble sort analysis and probability

Posted 13 December 2012 - 02:43 PM

```function [S] = bubblesortWMem(X)
% Bubble sort implementation with memory
len = length(X);
S=X;
% t0=tic;
sorted = false;
last=1;
while (~sorted)
sorted = true;

for i=last:len-1
if S(i) > S(i+1)
tmp = S(i);
S(i) = S(i+1);
S(i+1) = tmp;
sorted = false;
last=i;
end
end
end
% t1=toc;
% runtime = t0-t1;

```

```function [S] = bubblesortSE(X)
% Bubble sort implementation with Stop Early
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;
break
end
end
end
% t1=toc;
% runtime = t0-t1;

```

```function [S] = 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
% t1=toc;
% runtime = t0-t1;

```

```function [S] = bubblesortWMemSe(X)
% Bubble sort implementation with memory and Stop Early
len = length(X);
S=X;
% t0=tic;
sorted = false;
last=1;
while (~sorted)
sorted = true;

for i=last:len-1
if S(i) > S(i+1)
tmp = S(i);
S(i) = S(i+1);
S(i+1) = tmp;
sorted = false;
last=i;
break
end
end
end
% t1=toc;
% runtime = t0-t1

```

Its 4 different codes

This post has been edited by macosxnerd101: 14 December 2012 - 12:12 PM
Reason for edit:: Please use code tags

Reputation: 0
• Posts: 4
• Joined: 13-December 12

## Re: bubble sort analysis and probability

Posted 13 December 2012 - 03:19 PM

### #6 Skydiver

• Code herder

Reputation: 6166
• Posts: 21,264
• Joined: 05-May 12

## Re: bubble sort analysis and probability

Posted 14 December 2012 - 07:34 PM

What do you mean by calculate the probability of each while, if, and for statement?

Doesn't the fact that a particular keyword exists in an implementation indicate that it has a probability of 100%?

### #7 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12305
• Posts: 45,400
• Joined: 27-December 08

## Re: bubble sort analysis and probability

Posted 14 December 2012 - 09:08 PM

Algorithms aren't analyzed based on the probability of an if statement, but rather on their efficiency. Bubblesort has a runtime complexity of O(n^2), regardless of your implementations.