Thread: Is this BUBBLE SORT correct????

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    23

    Is this BUBBLE SORT correct????

    Hey im wondering what is wrong with this bubble sort code:
    Code:
      while (swapped){
                swapped=0;
                for(i=0; i<unsrtlen-1;i=i+1) {
                    if (array[i+1][4]<array[i][4]) {
                      for(j=0;j<5;j++) {
                      help=array[i][j];
                      array[i][j]=array[i+1][j];
                      array[i+1][j]=help;
                      swapped=1;
                    }
                   j=0;
                }
            }
               unsrtlen=unsrtlen-1;
               }
    this is just a part of my program....
    I have a 2d array of elements [10][5] and i want to sort the last element on the right hand side in ascending order which means that i have to move a whole line of elements up...
    It compiles fine but when i print it in a table i get a bunch of random numbers..
    Why is this so????
    Any help will be appreciated...

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    The outermost loop cycles thro' all the elements, from 0 to 9, of the 2D array.
    The innermost loop just swaps each of the 5 array elements conditionally.
    But it's missing a middle loop - one that compares adjacent elements.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by itCbitC View Post
    The outermost loop cycles thro' all the elements, from 0 to 9, of the 2D array.
    The innermost loop just swaps each of the 5 array elements conditionally.
    But it's missing a middle loop - one that compares adjacent elements.
    No it isn't. In this case the outmost loop is the while loop.

    Incidentally that while loop should be a do .. while, and this could be part of the problem. At the moment this code assumes that swapped is initially nonzero. Using a do..while loop is the correct thing to do whenever you find yourself speficially trying to cause the while condition to be true initially.
    Other notes:
    • Fix your indentation! Several of your closing brackets are currently misleadingly made to look like they close a different scope to what they do. This is the most important step.
    • The j=0; line serves no useful purpose and should be removed.
    • swapped does not need to be set to 1 every time through the j loop. Move it out to where j=0; is currently.
    • Does the output display okay when you don't include this piece of code? (except it's unsorted obviously)
    • Can you show a little more of the code, such as the code that displays the output?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by iMalc View Post
    No it isn't. In this case the outmost loop is the while loop.
    Whoa! good catch! tho' methinks it'd be more legible with another for loop.
    Code:
    for (i=0; i<10; i++) {
        for (k=0; k<10-i-1; k++) {
            if (array[k+1][4] < array[k][4]) {
                for(j=0; j<5; j++) {
                    ...
    Last edited by itCbitC; 04-28-2010 at 12:51 AM.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    23
    i sort of did figure it out.....
    what i didn't tell you is that this program has a max elements of [1000][5] and the input to this program might be 10 lines or 50 or 100 so i have that counter that counts the number of lines which is inputed
    I then placed the value of the counter in my printf statement resulting on only the scanned lines to show up in the table and not the empty elements.....
    NOW my probem was that the line that has the
    Code:
     for(i=0; i<unsrtlen-1;i=i+1) {
    the unsrtlen had to be replace with the counter -1 so that it only loops through the number of elements that was scanned.....
    thanks for ur help anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I bubble sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2008, 02:30 AM
  2. bubble sort help.
    By zeromx in forum C Programming
    Replies: 9
    Last Post: 10-30-2006, 07:37 AM
  3. Bubble sort
    By Lionmane in forum C Programming
    Replies: 5
    Last Post: 07-09-2005, 11:30 AM
  4. Problem with Bubble Sort code
    By lisa1234 in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2004, 03:40 PM
  5. Urgent - Bubble Sort problem
    By Bada Bing in forum C Programming
    Replies: 1
    Last Post: 11-15-2001, 05:56 AM