Thread: Bubble sort

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    207

    Bubble sort

    Hi! I'm attempting to create a bubble sort. By the way: a bubble sort is essentially 2 linear sorts at the same time, right?

    So far I've:

    1) Created a list of random numbers in an array
    2) Performed a linear sort from the beginning forward and the ending backward (at the same time)
    3) Set it up in a loop that's SUPPOSED to continue looping until each half is sorted. Any time a swap is performed, a flag is enabled that continues the loop. If the flag is not enabled, the program falls out of the loop (ie: stops sorting). This is the part I'm having trouble with. The flag seems to work correctly, but it doesn't break the loop.

    I haven't provided for numbers that are in the second half that belong in the first half (yet), but I'll get to that...

    Also I probably have a lot of redundant code since I'm still trying to make it work... ;-)

    Anyway, does anyone know why the WHILE condition is not being met?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    /*
    
    this generates random numbers and sorts them
    
    */
    
    //prototypes
    void sort_main (void);
    
    int main (void)
    {
    	sort_main ();
    	return 0;
    }
    
    void sort_main (void)
    {
    	int array[10];
    	int loop = 0;
    	int count = 0;
    	int max = 9;
    	int temp;
    	char cont = 'y';
    
    	//open output file for write
    	FILE *fp = fopen ("bubble_sort.txt", "w");
    
    	fprintf (fp,"Before:\n");
    
    	//initialize array values
    	for (loop = 0; loop <= max; loop++)
    	{
    		array[loop] = rand();
    		fprintf (fp,"\t%d", array[loop]);
    	}
    
    	fprintf (fp,"\n\nAfter:\n");
    
    	//check middle element if the number of elements is odd
    	if ((max + 1) % 2 != 0)
    	{
    		if (array[(max + 1) / 2] > array[(max + 1) / 2 + 1])
    		{
    			temp = array[(max + 1) / 2];
    			array[(max + 1) / 2 + 1] = array[max / 2];
    			array[(max + 1) / 2] = temp;
    			temp = 0;
    		}
    	}
    
    	do
    	{
    		cont = 'n';
    
    		for (loop = 0; loop < (max / 2); loop++)
    		{
    			//check from forward direction
    			if (array[loop] > array[loop + 1])
    			{
    				temp = array[loop + 1];
    				array[loop + 1] = array[loop];
    				array[loop] = temp;
    				temp = 0;
    				cont = 'y';
    			}
    			else if (array[loop] == array[loop + 1])
    			{
    				array[loop + 1] = 0;
    				cont = 'y';
    			}
    
    			//check from reverse direction
    			if (array[max - loop] < array[max - (loop + 1)])
    			{
    				temp = array[max - (loop + 1)];
    				array[max - (loop + 1)] = array[max - loop];
    				array[max - loop] = temp;
    				temp = 0;
    				cont = 'y';
    			}
    			else if (array[max - loop] == array[max - (loop + 1)])
    			{
    				array[max - (loop + 1)] = 0;
    				cont = 'y';
    			}
    		}
    
    		//print "cont" value and pause
    		printf ("%c\n", cont);
    		scanf ("%d", &temp);
    		temp = 0;
    
    	} while (cont = 'y');
    
    
    	for (loop = 0; loop <= max; loop++)
    	{
    		if (array[loop] != 0)
    		{
    			fprintf (fp,"\t%d", array[loop]);
    		}
    	}
    
    	fclose (fp);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    } while (cont = 'y');
    = is an assignment operator.
    == is the comparison operator.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    207
    Oh yeah! Ha! That was pretty dumb... :-)

    Thanks!

    mw

  4. #4
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    I often hear it preached that whenever you compare a variable to a constant, you should put the constant on the left side, as in ('y' == cont), so that if you forget the second equals sign, the compiler warns about the problem right away.

    I usually deliberately do the exact opposite, because annoying the preachy types can be amusing. (Also, every time I remember to put the constant value on the left side, I also remember that I need a double-equals sign.)

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Some compilers will sqawk about an assingment rather than a comparison being made in such situations, if you have the warnings turned on.

  6. #6
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    >I usually deliberately do the exact opposite

    I agree Rashakil, reversing the comparison reduces readability.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. bubble sort not working... wats d prob?
    By Huskar in forum C Programming
    Replies: 8
    Last Post: 03-31-2009, 11:59 PM
  3. My bubble sort only sorts once
    By Muller in forum C Programming
    Replies: 8
    Last Post: 03-27-2009, 04:36 PM
  4. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 04:48 AM
  5. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM