1. ## 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;
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. Code:
`} while (cont = 'y');`
= is an assignment operator.
== is the comparison operator.

Quzah. 3. Oh yeah! Ha! That was pretty dumb... :-)

Thanks!

mw 4. 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. Some compilers will sqawk about an assingment rather than a comparison being made in such situations, if you have the warnings turned on. 6. >I usually deliberately do the exact opposite

I agree Rashakil, reversing the comparison reduces readability. Popular pages Recent additions 