Thread: My bubble sort only sorts once

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    My bubble sort only sorts once

    Hi, i have been endlessly trying to make this bubble sort work. what happens is that i have to manually keep pressing 3 (this is my option to sort the data) for each swap, whereas i want the whole array sorted in one go. The function i have written is part of a CNC menu program where the user enters a number to perform a specific task. My bubble sort is shown below. The problem im having is that it is only swapping 2 numbers, then i press 3 and it sorts again etc. My question is how do i loop the whole thing to sort everything in one go?
    Many thanks
    EDIT: x is the number of values the user requests to be sorted

    Code:
    void bubblesortascending(float myarray[999][3],int x)
    	{
    		int i,countfrom;
    		float tempx,tempy,tempdrillsize;
    		countfrom=0;
    
    			for(i=1;i<=x-1;i++)
    			{
    				
    				countfrom++;
    				
    				if(myarray[i][0]>myarray[i+1][0])
    				{
    				tempx=myarray[i][0];
    				tempy=myarray[i][1];
    				tempdrillsize=myarray[i][2];
    				myarray[i][0]=myarray[i+1][0];
    				myarray[i][1]=myarray[i+1][1];
    				myarray[i][2]=myarray[i+1][2];
    				myarray[i+1][0]=tempx;
    				myarray[i+1][1]=tempy;
    				myarray[i+1][2]=tempdrillsize;
    				}
    			}
    		
    		
    		printf("To view the sorted data, please press 4 to display the array\n\r");
    		return;	
    	}
    Last edited by Muller; 03-27-2009 at 12:01 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your function can make more than one swap, but it can never make more than one pass through the list. Since bubble sort is an (x-1)-pass algorithm, you're short by several iterations. So you need to go through the array more times.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    ive tried to use a do while look to correct this but then my programs keeps crashing, is it correct to use a do while to make this work?
    for example i have an int called countfrom where countfrom=0, but how do i make it stop when the array has sorted x amount of times?
    Many thanks
    Ryan

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Muller View Post
    ive tried to use a do while look to correct this but then my programs keeps crashing, is it correct to use a do while to make this work?
    I suppose you can use one, but it's not the 'idiomatic' way. You know you're going to do the loop x-1 times, and people tend to use for when they know exactly how many times the loop is going to run.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Muller View Post
    Hi, i have been endlessly trying to make this bubble sort work...
    Maybe you should forget about trying to make it work and instead use selection sort, which is a far more efficient sorting algorithm.
    Just my 2c!

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    would someone mind showing me how i implement the do while into this bubble sort please.It keeps crashing, im struggling to find the while expression for when the program needs to stop. I tried two or loops but i still need to keep pressing the key each time for one swap until they are all done, thanks
    EDIT itCbitC, my teacher requires me to use a bubble sort for my assignment., but thank you anyway
    Ryan
    Last edited by Muller; 03-27-2009 at 01:19 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    do while is dead easy.
    Code:
    counter = 0;
    do {
    /* stuff */
    counter++;
    } while (counter < target);

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is the version of bubblesort that I use. It is *very* similar to your version, but does sort all the way through the items.

    Code:
    for(i = 0; i < MaxItems - 1; i++)  {
       for(j = i + 1; j < MaxItems; j++)  {
          if(Array[i] > Array[j])   {
             temp = Array[i];
             Array[i] = Array[j];
             Array[j] = temp;
          }
       }
    }
    
    //I see you're sorting a 2D array, so I'd use it like this:
    
    /* Sorts a 2D array of floats by using a 1D temp array*/
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MaxRows 4
    #define MaxCols 3
    
    float myarray[MaxRows][MaxCols] = {
    {3.2, 5.8, 1.6},
    {9.0, 7.7, 0.1},
    {4.6, 2.8, 1.3},
    {6.4, 8.5, 9.9} };
    
    void showIt(void);
    
    int main() {
       int i, j, row, col, MaxItems;
       float temp;
       float *Array;
    
       printf("\nOriginal Array: \n");
       showIt();
    
       //make a new 1D array the proper size
       Array = calloc(9, sizeof(float));  
    
    
       for(row = 0, i = 0; row < MaxRows; row++)
          for(col = 0; col < MaxCols; col++) {
             Array[i] = myarray[row][col];      //transfer myarray values into Array[]
             ++i;
          }
       //now sort with a great bubblesorter
    
       MaxItems = MaxRows * MaxCols;  //which should == i - 1
    
       for(i = 0; i < MaxItems - 1; i++)  {
          for(j = i + 1; j < MaxItems; j++)  {
             if(Array[i] > Array[j])   {
                temp = Array[i];
                Array[i] = Array[j];
                Array[j] = temp;
             }
          }
       }
    
    //And then transfer it right back to where it should be in myarray
    
       for(row = 0, i = 0; row < MaxRows; row++)  {
          for(col = 0; col < MaxCols; col++)  {
             myarray[row][col] = Array[i];
             ++i;
          }
       }
    
       printf("\nSorted Array: \n");
       showIt();
    
       printf("\n\n\t\t\t      press enter when ready \n");
       i = getchar();
       free(Array);
       return 0;
    
    }
    void showIt(void)  {
       int r, c;
    
       for(r = 0; r < MaxRows; r++)  {
          for(c = 0; c < MaxCols; c++)
             printf("%3.1f  ", myarray[r][c]);
          printf("\n");
       }
    }
    On small arrays, this is a very fast sorter.

    It looks like a lot of code, but it is astoundingly fast. I use this technique in my Sudoku solver. (except I use a static array, not a
    dynamic one.)
    Last edited by Adak; 03-27-2009 at 03:29 PM.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by itCbitC View Post
    Maybe you should forget about trying to make it work and instead use selection sort, which is a far more efficient sorting algorithm.
    Just my 2c!
    That all depends on how expensive it is to copy the items.
    If the item is something simple like an int, then an optimised bubblesort is usually faster.
    But both totally suck anyway really.
    Even the easy to implement "Comb sort" would blow them both out of the water for 999 items.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bubble sort not working... wats d prob?
    By Huskar in forum C Programming
    Replies: 8
    Last Post: 03-31-2009, 11:59 PM
  2. Bubble sort
    By Lionmane in forum C Programming
    Replies: 5
    Last Post: 07-09-2005, 11:30 AM
  3. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 04:48 AM
  4. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM
  5. optimizing bubble sort
    By Sargnagel in forum C Programming
    Replies: 14
    Last Post: 01-23-2003, 06:27 AM

Tags for this Thread