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

#1 kadaj6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 kadaj6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 kadaj6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 kadaj6  Icon User is offline

  • New D.I.C Head

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

Re: bubble sort analysis and probability

Posted 13 December 2012 - 03:19 PM

Please help
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • 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%?
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,603
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1