Bubble sort algorithm analysis

In summary: Imagine you have a sorted list, and then randomly insert a single item. You have to iterate over the entire list to get it into the right place. What if you have N items?In summary, the probability of the while loop being successful is dependent on the input data, specifically how sorted or unsorted it is. In the best case, the while loop will never execute and in the worst case, it will execute N-1 times. The average case is closer to the worst case, making the complexity of bubblesort O(N^2).
  • #1
kadaj6
31
0
Hello,

I need to analyze some bubble sort algorithm and calculate the probability of each conditional statements(if,for,while,ect...) be successful. I can post the algorithms if you need to see them.

Thanks in advance
 
Physics news on Phys.org
  • #2
We can't do this work for you but we could suggest how you might get the answers you need.

So show the algorithm and show how far you've gotten and what programming language you are using.
 
  • #3
function = 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;





function = 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 = 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 = 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;





i made a table with the amount of comparison ans assignments for each one, now i need the probability of each while,if, and for to be successful. I do not know how to do it.
 
  • #4
reformatting for readability using [ CODE ] [ /CODE ] tags (Mod: omit extra spaces)

kadaj6 said:
Code:
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;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;

i made a table with the amount of comparison ans assignments for each one, now i need the probability of each while,if, and for to be successful. I do not know how to do it.
 
Last edited by a moderator:
  • #5
Im not sure how to do this but I'd probably look at the if and think about my data

say I am sorting numbers 1 thru 5 but the are listed 5 thru 1

then your if will fire every time on the first pass on the second pass it depends.

now say I'm sorting 1 thru 5 but they are in order

then your if will never fire

Do you have some fixed data?

Another approach would be to insert counters in your code for each time the IF fired and for how many passes you make until the end is reached then print out the two numbers during each pass and at the end.
 
  • #6
Sounds like someone wants you to basically calculate the O() (big-O) notation for your sort. We will take a look at the last one, the straight bubble sort.

Code:
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;

You're going to have two answers, though generally we're only ever interested in the worst case.

In the best case, X is given to the function presorted. If this is the case, then your if statement is never going to be true. How many times then will it be executed? Wil sorted ever be set back to false after you set it to true?

In the worst case, X is given in reverse order. When that happens, how many items need to be moved, and how many places do they need to be moved? After 1 iteration how many items will be in the right location? After 2? After n?

A third answer is the average case, or random case, which for functions like bubblesort, is much closer to the worst case than the best case.
 

1. What is the bubble sort algorithm?

The bubble sort algorithm is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. This process is repeated until the list is sorted.

2. How does the bubble sort algorithm work?

The bubble sort algorithm works by comparing adjacent elements in a list and swapping them if they are in the wrong order. This process is repeated until the entire list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list in each iteration.

3. What is the time complexity of the bubble sort algorithm?

The time complexity of the bubble sort algorithm is O(n^2), meaning that the time it takes to sort a list of n elements is proportional to the square of the number of elements. This makes it an inefficient algorithm for large lists, as the time it takes to sort increases significantly with the size of the list.

4. What are the advantages of using the bubble sort algorithm?

The main advantage of the bubble sort algorithm is its simplicity. It is easy to understand and implement, making it a good choice for educational purposes or for sorting small lists. It also has a small memory footprint, as it only requires a single additional element to hold the temporary value during swapping.

5. What are the limitations of the bubble sort algorithm?

The main limitation of the bubble sort algorithm is its time complexity, which makes it inefficient for sorting large lists. It also has a relatively high number of comparisons and swaps, which can make it slower than other sorting algorithms. Additionally, it is not suitable for sorting lists that contain a large number of duplicate elements, as it does not take into account the original order of these elements.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
11
Views
922
  • Engineering and Comp Sci Homework Help
Replies
16
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
933
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
523
  • Engineering and Comp Sci Homework Help
Replies
4
Views
4K
  • General Discussion
Replies
6
Views
921
  • Programming and Computer Science
Replies
8
Views
282
  • Engineering and Comp Sci Homework Help
Replies
25
Views
3K
Back
Top