# My bubble sort only sorts once

• 03-27-2009
Muller
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;                }```
• 03-27-2009
tabstop
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.
• 03-27-2009
Muller
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
• 03-27-2009
tabstop
Quote:

Originally Posted by Muller
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.
• 03-27-2009
itCbitC
Quote:

Originally Posted by Muller
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!
• 03-27-2009
Muller
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
• 03-27-2009
tabstop
Code:

```counter = 0; do { /* stuff */ counter++; } while (counter < target);```
• 03-27-2009
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.)
• 03-27-2009
iMalc
Quote:

Originally Posted by itCbitC
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.