Results 1 to 3 of 3
  1. #1
    Gnabster is offline Member
    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Default How to understand Bubble Sort Pseudo code?

    Hi,

    I have difficulity understanding the Pseudo code compared to Java, i had cracked my head but still dont understand the coding, just a few lines but i couldn't get myself to fully understand.

    The Pseudo Code:

    for i=1 to n-1 do
    for j=i to n-1 do
    if x[j]>x[j+1] then
    temp=x[j]
    x[j]=x[j+1]
    x[j+1]=temp
    end {if}
    end {for}
    end {for}


    From my understanding of this bubble sort code:

    int[] x = {5, 4, 3, 2, 1};
    int n = x.length;
    int temp;
    int counter = 0;

    for (int i = 0; i < n; i++) {
    // counter ++;
    for (int j = i; j < n; j++) {

    if (x[j] > x[j + 1]) {
    temp = x[j];
    x[j] = x[j + 1];
    x[j + 1] = temp;
    counter++;
    }
    }
    }

    for (int i = 0; i < x.length; i++) {
    System.out.print(x[i] + " ");
    }

    System.out.println();
    System.out.println(counter);


    My question is:

    Is the Pseudo Code same as the other bubble sort code online? This do not have the true/false checker.
    I need to calculate the:

    1) Best case
    2) Worst case
    3) Average case

    for the pseudo code, in terms of Big O Notation, all bubble sort should be worst case Big O N square, best is Big O(n), average is same as worst case.
    However i am unable to convince myself by looking at the pseudo code alone, any kind souls please assist if you understand the Pseudo code, Thanks in advance

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: How to understand Bubble Sort Pseudo code?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to understand Bubble Sort Pseudo code?

    Quote Originally Posted by Gnabster View Post
    for the pseudo code, in terms of Big O Notation, all bubble sort should be worst case Big O N square, best is Big O(n), average is same as worst case.
    However i am unable to convince myself by looking at the pseudo code alone, any kind souls please assist if you understand the Pseudo code, Thanks in advance
    Best, average and worst case are all O(n^2); if you're just counting the number of swaps the best case is O(0) (no swaps needed if the array is already sorted), but the average and worst case are still O(n^2).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Bubble Sort For 2D Arrays
    By tmantonym in forum New To Java
    Replies: 2
    Last Post: 11-30-2011, 09:40 PM
  2. Problem with bubble sort code
    By flyingcurry in forum New To Java
    Replies: 4
    Last Post: 11-11-2010, 03:14 AM
  3. Bubble sort
    By pineapple in forum New To Java
    Replies: 3
    Last Post: 04-25-2009, 12:45 AM
  4. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  5. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 05:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •