Results 1 to 7 of 7

Thread: BuBBle Sorting

  1. #1
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default BuBBle Sorting

    Hey guy basic bubble sorting . I 've triple checked code and still cant find mistake. Output is 45.37,22,16,99
    Java Code:
    public class Sort { 
    
    	public static void main(String[] args) {
    	int i =0;
    	
    
    	int[] list = {45,37,22,16,99};
    	for(i = list.length-1; i < 0; i--){
    		for(int k =0 ; k <list.length-1; k++ ){
    		int tempa = list[k];
    		int tempb = list[k+1];
    			if (list[k] > list[k+1]){
    		 	list[k] = tempb;
    		 	list[k+1] = tempa;
    			
    				}
    				}
    			}
    			
    			for(int x =0; x < list.length; x ++) {
    			System.out.println(list[x] + "");
    			}
    		}
    	}

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    Java Code:
    for(i = list.length-1; i < 0; i--){
    Look closely at the above loop that's defined in your code. Imagine an array of any length, what will happen when this executes (answer: nothing)

  3. #3
    Join Date
    Mar 2011
    Posts
    94
    Rep Power
    0

    Default

    Your other for loop isn't correct either:
    Java Code:
    for(int k =0 ; k <list.length-1; k++ ){
    This loop is not counting the way it should be for bubblesort.

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    as doWhile is telling you, i > 0; not < 0.

  5. #5
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    This is you may try...

    Java Code:
    for(int i = 0; i < list.length-1; i++)
      for(int j = i+1; j < list.length; j++)
         {
             if(list[i]>list[j])
              {
                temp=list[i];
                list[i]=list[j];
                list[j]=temp;
              }
          }

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    ...and a perfect example of spoonfeeding

  7. #7
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    Hi Guyz first off THANKS.. I took "DOwhile" suggestion. I fixed the loop and it corrected the problem. works fine now.

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. Best way to do a pop-up 'info bubble'
    By lightstream in forum AWT / Swing
    Replies: 2
    Last Post: 01-27-2009, 07:39 AM
  4. Trouble w/ Bubble Sort
    By bri1547 in forum New To Java
    Replies: 4
    Last Post: 08-01-2008, 04:41 PM
  5. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 05:27 PM

Posting Permissions

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