Bubble sort

This is a discussion on Bubble sort within the C Programming forums, part of the General Programming Boards category; Hi I have recently started C and i am trying to get my program to sort 10 numbers in an ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    4

    Bubble sort

    Hi

    I have recently started C and i am trying to get my program to sort 10 numbers in an array i have it so that it will sort them in ascending order but i dont know how to do it so it will display at the same time descending order, my code is as follows,

    Code:
    #include <stdio.h>
    #define NUM 10 /* This means that 10 integers will be re aranged*/
    
    main()
    {
       int aa, bb, cc, dd;
       int vv[NUM];
    
       printf("Please enter %d integers to be sorted\n", NUM);
       for ( aa = 0; aa > NUM; aa++ )  
       {
          printf("Enter an integer %2d : ", aa+1 );
          scanf("%d", &vv[aa]);
       }
    
        for ( bb = NUM-1; bb < 0; bb-- )
       {
         dd = 0;
    
    	 for ( aa = 0; aa > bb; aa++ )
         {
            if ( vv[aa] <
    			vv[aa+1] )
            {
               /* Swap values */
               cc = vv[aa];
               vv[aa] = vv[aa+1];
               vv[aa+1] = cc;
               dd++;   /* Increment counter */
    		   
            }
    		
         }
    
    
    	  if ( dd == 0 )
         {
           break;
    
    	    }
       } 
    
    	printf("\nThe new order of numbers is :\n");
       for ( aa = 0; aa < NUM; aa++ )
       {
          printf("New order: %2d is %d\n", aa+1, vv[aa]);
       }
       printf("\n\n");
       return 0;
    
    
    }
    I can not find any help in my book "C Programming in easy step"
    Thanks, please note, i am a novice, so i may not understand the technical terms, thanks!
    Last edited by Loooke; 12-08-2004 at 01:59 PM.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Hey there, I found your problem. You were getting the > and < the wrong way round. a > b means "a is greater than b" and would be true if a was 5 and b was 2. Pretty complicated algorithm to be starting with don't you think?

    Here's your fixed code.

    Code:
    #include <stdio.h>
    #define NUM 10 /* This means that 10 integers will be re aranged*/
    
    int main(void)
    {
       int aa, bb, cc, dd;
       int vv[NUM];
    
       printf("Please enter %d integers to be sorted\n", NUM);
       for ( aa = 0; aa < NUM; aa++ )  
       {
          printf("Enter an integer %2d : ", aa+1 );
          scanf("%d", &vv[aa]);
       }
    
       for ( bb = NUM-1; bb > 0; bb-- )
       {
         dd = 0;
    
    	 for ( aa = 0; aa < bb; aa++ )
         {
            if ( vv[aa] > vv[aa+1] )
            {
               /* Swap values */
               cc = vv[aa];
               vv[aa] = vv[aa+1];
               vv[aa+1] = cc;
               dd++;   /* Increment counter */
    		   
            }
    		
         }
    
    	  if ( dd == 0 )
           break;
    
       } 
    
    	printf("\nThe new order of numbers is :\n");
       for ( aa = 0; aa < NUM; aa++ )
       {
          printf("New order: %2d is %d\n", aa+1, vv[aa]);
       }
       printf("\n\n");
    
       return 0;
    }
    Last edited by Brian; 12-08-2004 at 02:12 PM.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    4
    Hi

    Thanks for the response, im getting the same outcome, im not sure if i stated exactly what i need, but i need the arrays to be sorted in

    ascending and descending order, and have 2 lots of results displayed at the same time,

    thanks again!

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    It works for me, this is the result I get using the code I posted

    Code:
    Please enter 10 integers to be sorted
    Enter an integer  1 : 42
    Enter an integer  2 : 23
    Enter an integer  3 : 12
    Enter an integer  4 : 16
    Enter an integer  5 : 94
    Enter an integer  6 : 21
    Enter an integer  7 : 5
    Enter an integer  8 : 33
    Enter an integer  9 : 2
    Enter an integer 10 : 10
    
    The new order of numbers is :
    New order:  1 is 2
    New order:  2 is 5
    New order:  3 is 10
    New order:  4 is 12
    New order:  5 is 16
    New order:  6 is 21
    New order:  7 is 23
    New order:  8 is 33
    New order:  9 is 42
    New order: 10 is 94
    
    
    Press any key to continue . . .

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    4
    Quote Originally Posted by Brian
    It works for me, this is the result I get using the code I posted

    Code:
    Please enter 10 integers to be sorted
    Enter an integer  1 : 42
    Enter an integer  2 : 23
    Enter an integer  3 : 12
    Enter an integer  4 : 16
    Enter an integer  5 : 94
    Enter an integer  6 : 21
    Enter an integer  7 : 5
    Enter an integer  8 : 33
    Enter an integer  9 : 2
    Enter an integer 10 : 10
    
    The new order of numbers is :
    New order:  1 is 2
    New order:  2 is 5
    New order:  3 is 10
    New order:  4 is 12
    New order:  5 is 16
    New order:  6 is 21
    New order:  7 is 23
    New order:  8 is 33
    New order:  9 is 42
    New order: 10 is 94
    
    
    Press any key to continue . . .
    Hi
    yeh thats what i get aswell, but the new order needs to display the results in descending order as well as ascending :-S

    ta

  6. #6
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    oh try this then:

    Code:
    #include <stdio.h>
    #define NUM 10 /* This means that 10 integers will be re aranged*/
    
    int main(void)
    {
       int aa, bb, cc, dd;
       int vv[NUM];
       int dec[NUM];
    
       printf("Please enter %d integers to be sorted\n", NUM);
       for ( aa = 0; aa < NUM; aa++ )  
       {
          printf("Enter an integer %2d : ", aa+1 );
          scanf("%d", &vv[aa]);
       }
    
       for ( bb = NUM-1; bb > 0; bb-- )
       {
         dd = 0;
    
    	 for ( aa = 0; aa < bb; aa++ )
         {
            if ( vv[aa] > vv[aa+1] )
            {
               /* Swap values */
               cc = vv[aa];
               vv[aa] = vv[aa+1];
               vv[aa+1] = cc;
               dd++;   /* Increment counter */
    		   
            }
    		
         }
    
    	  if ( dd == 0 )
           break;
    
       } 
       
       bb = NUM - 1;
       /* fill a new array with the results in descending order */
       for ( aa = 0; aa < NUM; aa++ ) {
           dec[aa] = vv[bb];
           bb --;
       }   
    
    	printf("\nThe new order of numbers is :\n");
       for ( aa = 0; aa < NUM; aa++ )
       {
          printf("New order: %2d is %d and %d\n", aa+1, vv[aa], dec[aa]);
       }
       printf("\n\n");
       system("pause");
       
       return 0;
    }
    gives this result:
    Code:
    Please enter 10 integers to be sorted
    Enter an integer  1 : 92
    Enter an integer  2 : 52
    Enter an integer  3 : 10
    Enter an integer  4 : 24
    Enter an integer  5 : 68
    Enter an integer  6 : 29
    Enter an integer  7 : 15
    Enter an integer  8 : 63
    Enter an integer  9 : 29
    Enter an integer 10 : 8
    
    The new order of numbers is :
    New order:  1 is 8 and 92
    New order:  2 is 10 and 68
    New order:  3 is 15 and 63
    New order:  4 is 24 and 52
    New order:  5 is 29 and 29
    New order:  6 is 29 and 29
    New order:  7 is 52 and 24
    New order:  8 is 63 and 15
    New order:  9 is 68 and 10
    New order: 10 is 92 and 8
    
    
    Press any key to continue . . .

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    4
    Thank you very much, i appreciate your help, Could you suggest some decent books i should look into purchasing for someone with my knowledge as to be honest there are so many around and the one i have chosen i find to be usless!

    thanks again for your help!

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Or you can forget the second array and all its associated variables and just say:

    Code:
    printf("\nThe new order of numbers is :\n");
    for ( aa = 0; aa < NUM; aa++ )
    {
        printf("New order: %2d is %d and %d\n", aa+1, vv[aa], vv[NUM-aa-1]);
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Quote Originally Posted by Loooke
    Thank you very much, i appreciate your help, Could you suggest some decent books i should look into purchasing for someone with my knowledge as to be honest there are so many around and the one i have chosen i find to be usless!

    thanks again for your help!
    No problem.

    This book has a short tutorial and a good reference on the standard library, it is considered "the" book on c, and covers all of the basics.

    for a full tutorial I recommend http://www.amazon.com/exec/obidos/tg...58565?v=glance. Although I haven't read it, the C++ version was very thorough.

  10. #10
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Quote Originally Posted by hk_mp5kpdw
    Or you can forget the second array and all its associated variables and just say:
    I need more sleep

Popular pages Recent additions subscribe to a feed

Similar Threads

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21