# Thread: Problem with Bubble Sort code

1. ## Problem with Bubble Sort code

Code:
```#include<stdio.h>
#include<conio.h>
#define PF printf
/*PROGRAM TO BUBBLE SORT & 'n'NUMBERS INPUT BY USER*/
void main()
{
int nos,temp;
int a =1,b=1;
int x;

PF("\n\t\t\tLISA'S NUMBER BUBBLE SORTER IN ASCENDING ORDER");
PF("\nHOW MANY NUMBERS : ");
scanf("%d",&x);
x = x+1;
while(a <x)
{
PF("\nEnter Number %d : ",a);
scanf("%d",&nos[a]);
a++;
}

PF("\nTHE ORIGINAL SET OF NUMBERS:-");
a =1;
while(a < x)
{

cprintf("\n%d",nos[a]);
a++;
}
a=1;
//////////////////////////Initiate sorting////////////////////////
b =1;
for(b=1;b<x;b++){
for(a =1; a<x;a++){
if(nos[a] > nos[a+1]){temp[a] = nos[a];nos[a] = nos[a+1];nos[a+1] = temp[a];}
}
}
getch();		a = 1;
cprintf("\nAFTER BUBBLE SORTING:-");
while(a <x){
cprintf("\n%d",nos[a]); 		a++;}
getch();
}
//////////////////END of Program///////////////////////////////////```
Should I use the special no. "-1" somewhere to end the sort???  2. Not until you figure out how to format code properly - this means indentation.

main() returns an int (not void)
Please don't use the preprocessor to rename variables (PF=printf) I made a program that sorts a set of numbers in a file to integars using either bubble,insertion or selection sort....but i'm not sure if i should post it or help lisa fix hers....coz i suck at fixing other'sw work.

EDIT: My bad...I cant find the int sorter but i found the string sorter... 4. >#define PF printf
Eew, that is awful. What purpose does redefining printf serve?

>void main()
int main()

Compare this with your code, it will be more instructive than us simply telling you the problems. I have written it in C since that seems to be your choice of language even though this is posted in the C++ forum:
Code:
```#include <stdio.h>
#include <stdlib.h>

void bubble_sort ( int *a, int size )
{
int i, j;

for ( i = size - 1; i >= 0; i-- ) {
for ( j = 0; j < i; j++ ) {
if ( a[j] > a[j + 1] ) {
int save = a[j];
a[j] = a[j + 1];
a[j + 1] = save;
}
}
}
}

int main ( void )
{
int *numbers;
int n;
int i, j;

printf ( "Julienne's number bubble sorter in ascending order\n" );
printf ( "How many numbers: " );
fflush ( stdout );
if ( scanf ( "%d", &n ) != 1 || n < 1 ) {
fprintf ( stderr, "Invalid input\n" );
return EXIT_FAILURE;
}
numbers = malloc ( n * sizeof *numbers );
if ( numbers == NULL ) {
fprintf ( stderr, "Memory allocation error\n" );
return EXIT_FAILURE;
}
for ( i = 0; i < n; i++ ) {
printf ( "Number %d: ", i + 1 );
fflush ( stdout );
if ( scanf ( "%d", &numbers[i] ) != 1 )
break;
}
bubble_sort ( numbers, i );
for ( j = 0; j < i; j++ )
printf ( "%d ", numbers[j] );
printf ( "\n" );
free ( numbers );

return EXIT_SUCCESS;
}``` 5. All was quiet until nomi bumped a month-old thread
There seems to be a rash of bumping going on at the moment. 6. >All was quiet until nomi bumped a month-old thread
Urf, how annoying. I suppose I will have to watch for this lest I waste my time answering an old thread again. 7. replace
for(a =1; a<x;a++)

with this one

for(a =1; a<x-1;a++)

wont give any logical errors more
------------------  Popular pages Recent additions 