a little help with bubble sort

This is a discussion on a little help with bubble sort within the C Programming forums, part of the General Programming Boards category; Hi all, i did a search on google with bubble sort to read up on the algorithm and it sounds ...

  1. #1
    cman
    Guest

    a little help with bubble sort

    Hi all, i did a search on google with bubble sort to read up on the algorithm and it sounds easy enough but i just have a question on it regarding the swapping of numbers in the arrays..

    Code:
    for ( j = 1; j <= 4; j++ )
       for( i = 0; i <= 3; i++ )
    
    if ( a[ i ] > a[ i + 1 ] ) {
    hold = a[ i ];
    a[ i ] = a[ i + 1 ];
    a[ i + 1 ] = hold;
    }
    if i have say 5 numbers 7, 19, 10, 5, 89
    after the first pass 7 gets compared to 19 and since its false it stays.
    so then it does a second pass? and compare 19 with 10 which is true.. so
    hold = 19;
    19 = 10;
    10=hold;

    so at this point in time do the array values change? so 19 and 10 get swapped around? then a 3rd pass begins comparing 19 and 5?

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    first pass:
    (7 19) 10 5 89 -- compare those
    7 (19 10) 5 89 -- compare those (swap)
    7 10 (19 5) 89 -- compare those (swap)
    7 10 5 (19 89) -- compare those

    second pass:
    (7 10) 5 19 89 -- compare those
    7 (10 5) 19 89 -- compare those (swap)
    7 5 (10 19) 89 -- compare those
    7 5 10 (19 89) -- compare those

    etc..
    there are only 10 people in the world, those who know binary and those who dont

  3. #3
    cman
    Guest
    I think ive got the idea now, and also how would i trace this program? like show each step of the way until it ends? How can i change the code below to output a trace.

    Code:
    #include <stdio.h>
    #define SIZE 10
    
    int main()
    {
    	int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
    	int i, pass, hold;
    
    	printf( "Data items in original order\n" );
    
    	for ( i = 0; i <= SIZE - 1; i++ )
    		printf( "%4d", a[ i ] );
    
    	for ( pass = 1; pass <= SIZE - 1; pass++ )
    
    		for( i = 0; i <= SIZE - 2; i++ )
    		printf( "%d %d ", a[ i ], hold );
    		if ( a[ i ] > a[ i + 1 ] ) {
    		hold = a[ i ];		
    		a[ i ] = a[ i + 1 ];	
    		a[ i + 1 ] = hold;	
    	}	
    		
    	printf( "\nData items in ascending order\n" );
    
    	for ( i = 0; i <= SIZE - 1; i++ )
    		printf( "%4d", a[ i ] );
    
    	printf( "\n" );
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    there are only 10 people in the world, those who know binary and those who dont

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. bubble sort not working... wats d prob?
    By Huskar in forum C Programming
    Replies: 8
    Last Post: 03-31-2009, 11:59 PM
  3. How do I bubble sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2008, 01:30 AM
  4. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 04:48 AM
  5. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 03:54 PM

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