# Help with Bi-Directional Bubble Sort in C

• 04-19-2002
cunninglinguist
Help with Bi-Directional Bubble Sort in C
I need to implement a bi-directional bubble sort algo that will sort 12 randomly generated integers of an array. I am able to generate and print the 12 random integers, as well as sort them in their final ascending order. However, the program also needs to print each pass of the array as it is sorting the integers ( see sample output to clear this up). The "<<" and ">>" markers you see in the output serve to show which integers of the array are already in the final sorted position and therefore don't need to be checked. This is to ensure that there are as few passes as possible (for efficiency). Here's the code I have so far.

Code:

``` #include <stdio.h> #include <stdlib.h> #include <math.h> #include <limits.h> #define MAX_ARRAY_SIZE 10000 enum BOOLEAN {FALSE, TRUE}; void biDirBubSort(long thearray[], int sizeofarray); int main() {   int i, unsorted[12];   /* seed the random number generator */   srand(time(NULL));   /* generate the random elements */   for (i = 0; i < 12; ++i)     unsorted[i] = rand() % 32;   /* print the 50 elements of the unsorted array, 10 per line */   printf("Elements of the unsorted array\n");   for (i = 0; i < 12; ++i)   {       printf("%4d", unsorted[i]);   }   /* sort the 50 elements of unsorted into sorted */   biDirBubSort(unsorted, 12);   /* print the 50 elements of the sorted array, also 10 per line */   printf("\n\nElements of the sorted array\n");   for (i = 0; i < 12; ++i)   {       printf("%4d", unsorted[i]);   }   return 0; } void biDirBubSort(long thearray[], int sizeofarray) { int j; long temp; int limit = sizeofarray; int st = -1; enum BOOLEAN swapped; while(st < limit)     {     swapped = FALSE;     st++;     limit--;     for(j=st; j<limit; j++)              // Bubble the next largest one up         {         if(thearray[j] > thearray[j+1])             {             temp = thearray[j];             thearray[j] = thearray[j+1];             thearray[j+1] = temp;             swapped = TRUE;             }         }     if(!swapped)                        // Quit if the array is already sorted         { return; }     swapped = FALSE;     for(j=limit; j>=st; --j)            // Bubble the next smallest one down         {         if (thearray[j]>thearray[j + 1])             {             temp = thearray[j];             thearray[j] = thearray[j+1];             thearray[j+1] = temp;             swapped = TRUE;             }       }     if(!swapped)                        // Quit if the array is already sorted       { return; }     } return; }```

Here's some sample output:

Elements of the unsorted array
20 12 5 12 11 7 25 1 1 2 27 4

Sorting

<< 12 5 12 11 7 20 1 1 2 25 4 >> 27
1 << 12 5 12 11 7 20 1 2 4 25 >> 27
1 << 5 12 11 7 12 1 2 4 20 >> 25 27
1 1 << 5 12 11 7 12 2 4 20 >> 25 27
1 1 << 5 11 7 12 2 4 12 >> 20 25 27
1 1 2 << 5 11 7 12 4 12 >> 20 25 27
1 1 2 << 5 7 11 4 12 >> 12 20 25 27
1 1 2 4 << 5 7 11 12 >> 12 20 25 27
1 1 2 4 << 5 7 11 >> 12 12 20 25 27

Elements of the sorted array
1 1 2 4 5 7 11 12 12 20 25 27

Basically, I have parts one and three done with the code I have so far. Can anyone show me how to go about printing each pass of the array as it is sorting and how to place the "<<" and ">>" markers in their proper places.

I bowdown to all those that can help.

Thanks.