bubble sort directional problems

This is a discussion on bubble sort directional problems within the C Programming forums, part of the General Programming Boards category; I want to sort an array using bubble sort. I know it's the slowest but i have only 5-10 elements ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    74

    bubble sort directional problems

    I want to sort an array using bubble sort. I know it's the slowest but i have only 5-10 elements so it's fast enough. My algorithm for bubble sort works cause if I code this

    Code:
    void Bubble(int array[], int length, int direction)
    {
    	int i, j;
    	
    	for (i=0; i<(length-1); i++) 
    	{
    		for (j=0; j<(length-i-1); j++) 
    		{
    			if (array[j] > array[j+1])
    				 Swap(&array[j], &array[j+1]);
    		}
    	}
    }
    for the array
    Code:
    884, 275, 470, 890, 860
    i get as expected
    Code:
    275, 470, 860, 884, 890

    I also want the user to determine in which direction the array is sorted, up or down, but here is the problem because I get a stupid response.

    Code:
    void Bubble(int array[], int length, int direction)
    {
    	int i, j;
    	
    	for (i=0; i<(length-1); i++) 
    	{
    		for (j=0; j<(length-i-1); j++) 
    		{
    			if (direction == 1)
    				if (array[j] > array[j+1])
    				   Swap(&array[j], &array[j+1]);
    			else 
    				if (array[j] < array[j+1]) 
    					Swap(&array[j], &array[j+1]); 
    		}
    	}
    }
    but now i get this
    Code:
    860, 890, 470, 275, 884
    and it's ming boggling why!!
    Last edited by budala; 01-21-2010 at 11:54 AM.

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    18
    Quote Originally Posted by budala View Post

    Code:
    			if (direction == 1)
    				if (array[j] > array[j+1])
    				   Swap(&array[j], &array[j+1]);
    			else 
    				if (array[j] < array[j+1]) 
    					Swap(&array[j], &array[j+1]);
    Hey,

    the C compiler always binds the "else" to the inner-most if, so that your else is not bound to the "if (direction == 1)". Your indention however easily leads to the assumption that your code must be correct. Instead rewrite your code as follows:

    Code:
    			if (direction == 1)
                            {
    				if (array[j] > array[j+1])
    				   Swap(&array[j], &array[j+1]);
                            }
    			else
                            { 
    				if (array[j] < array[j+1]) 
    					Swap(&array[j], &array[j+1]); 
                            }
    }
    - Andi -

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I bubble sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2008, 02:30 AM
  2. Bubble sort
    By Lionmane in forum C Programming
    Replies: 5
    Last Post: 07-09-2005, 12:30 PM
  3. Help With Bubble Sort
    By Explicit in forum C Programming
    Replies: 7
    Last Post: 05-28-2004, 09:46 AM
  4. Sorting a string using bubble sort?!
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 05:44 PM
  5. bubble sort array of strings
    By the_head in forum C Programming
    Replies: 1
    Last Post: 04-02-2002, 05:15 PM

Tags for this Thread


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