Results 1 to 5 of 5
  1. #1
    flyingcurry is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default Problem with bubble sort code

    The method below for bubble sort should work, it displays the unsorted array fine, but for the sorted array it only displays one of the numbers. It seems to have just skipped over the other elements in the array.

    Here is a sample output:
    How many random even integers would you like to generate? 6
    Unsorted:
    196
    166
    68
    14
    68
    176
    Sorted:
    196
    5


    Java Code:
    public static void bubbleSort (int [] array, int length) {
    
    
    		int i, j,t=0;
    		System.out.println("Unsorted: ");
    		for (i=0; i<array.length; i++)
    		{
    			System.out.println(array[i]);
    
    		}
    		for(i = 0; i < length; i++){
    			for(j = 1; j < (length-i); j++){
    				if(array[j-1] > array[j]){
    					t = array[j-1];
    					array[j-1]=array[j];
    					array[j]=t;
    				}
    			}
    
    			System.out.println("Sorted: ");
    			for (i=0; i<length-1; i++);
    			{
    				System.out.println(array[i]);
    				System.out.println(i);
    			}
    		}
    	}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    flyingcurry is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    What is the value of "length"?
    Oh, length just stands for the array.length

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by flyingcurry View Post
    The method below for bubble sort should work, it displays the unsorted array fine, but for the sorted array it only displays one of the numbers. It seems to have just skipped over the other elements in the array.

    Here is a sample output:
    How many random even integers would you like to generate? 6
    Unsorted:
    196
    166
    68
    14
    68
    176
    Sorted:
    196
    5


    Java Code:
    public static void bubbleSort (int [] array, int length) {
    
    
    		int i, j,t=0;
    		System.out.println("Unsorted: ");
    		for (i=0; i<array.length; i++)
    		{
    			System.out.println(array[i]);
    
    		}
    		for(i = 0; i < length; i++){
    			for(j = 1; j < (length-i); j++){
    				if(array[j-1] > array[j]){
    					t = array[j-1];
    					array[j-1]=array[j];
    					array[j]=t;
    				}
    			}
    
    			System.out.println("Sorted: ");
    			for (i=0; i<length-1; i++);
    			{
    				System.out.println(array[i]);
    				System.out.println(i);
    			}
    		}
    	}
    Do you want to print out the 'sorted' array while you are sorting it? i.e. print it out after each sorting step? Note that the last loop also changes the variable 'i' (as the outer loop does). Use another variable for the last loop. btw that variable 'length' is redundant, i.e. your array itself 'knows' its length: array.length. Also what is the semicolon doing here?

    Java Code:
    for (i=0; i<length-1; i++);
    ... remove it.

    kind regards,

    Jos

  5. #5
    flyingcurry is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Do you want to print out the 'sorted' array while you are sorting it? i.e. print it out after each sorting step? Note that the last loop also changes the variable 'i' (as the outer loop does). Use another variable for the last loop. btw that variable 'length' is redundant, i.e. your array itself 'knows' its length: array.length. Also what is the semicolon doing here?
    Thanks, i realized after that making a variable for array.length was unnecessary.
    The semi-colon was a bad typing mistake.

    I changed the use of 'i' so that i is declared separatedly within each loop like "for int i" instead of one i used by every loop, and the bubble sort seems to work fine right now.

    Thanks Jos.

Similar Threads

  1. Question with bubble sort
    By Metastar in forum New To Java
    Replies: 22
    Last Post: 09-13-2010, 06:25 AM
  2. Bubble sort
    By pineapple in forum New To Java
    Replies: 3
    Last Post: 04-25-2009, 12:45 AM
  3. Trouble w/ Bubble Sort
    By bri1547 in forum New To Java
    Replies: 4
    Last Post: 08-01-2008, 04:41 PM
  4. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  5. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 05:27 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •