Thread: Problem with Bubble Sort code

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    4

    Problem with Bubble Sort code

    Can anyone please help me to fix the following 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???

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    TAlking about sorting.

    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. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >#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;
    }
    My best code is written with the delete key.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    All was quiet until nomi bumped a month-old thread
    There seems to be a rash of bumping going on at the moment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    My best code is written with the delete key.

  7. #7
    Registered User justdoit22's Avatar
    Join Date
    Dec 2003
    Posts
    18
    replace
    for(a =1; a<x;a++)

    with this one

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


    wont give any logical errors more
    ------------------
    raeally an old thread

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    There is a bubblesort example in the faq in the pointer tutorial.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Bubble Sort Query
    By coolboarderguy in forum C Programming
    Replies: 2
    Last Post: 04-15-2008, 12:50 AM
  3. optimizing bubble sort
    By Sargnagel in forum C Programming
    Replies: 14
    Last Post: 01-23-2003, 06:27 AM
  4. bubble sort
    By lambs4 in forum C Programming
    Replies: 3
    Last Post: 06-03-2002, 05:46 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM