Problem with Bubble Sort code

• 12-14-2003
lisa1234
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[200],temp[200]; 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???:confused:
• 12-14-2003
Salem
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)
• 01-12-2004
nomi

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...
• 01-13-2004
Prelude
>#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; }```
• 01-13-2004
Salem
All was quiet until nomi bumped a month-old thread
There seems to be a rash of bumping going on at the moment.
• 01-13-2004
Prelude
>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.
• 01-13-2004
justdoit22
replace
for(a =1; a<x;a++)

with this one

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

wont give any logical errors more
------------------