Results 1 to 4 of 4
  1. #1
    Java Tip's Avatar
    Java Tip is offline Moderator
    Join Date
    Nov 2007
    Posts
    1,694
    Blog Entries
    430
    Rep Power
    10

    Default How to sort a list using Bubble sort algorithm

    This code is the implementation of the bubble sort algorithm.

    Java Code:
    public class BubbleSortExp {
    
    	public static void main(String[] args) {
    		int s[] = { 23, 12, 466, 22, 1 };
    		int temp;
    		for (int i = 0; i < 4; i++) {
    			for (int j = 0; j < 4 - i; j++) {
    				if (s[j + 1] < s[j]) {
    					temp = s[j + 1];
    					s[j + 1] = s[j];
    					s[j] = temp;
    				}
    			}
    		}
    		System.out.println("Sorted Array");
    		for (int i = 0; i < s.length; i++) {
    			System.out.println(s[i]);
    		}
    		System.out.println(s);
    	}
    
    }

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Ewww... :)

    The dreaded and most fundamental, good for a small number of elements, but horrible for larger than.. say.. 10 elements? :D

    What else you got for us Chief ? ;)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    What you mean Captain, its work fine for 15 numbers also. :)

    I'm worried why did you say that for 10 elements it is horrible. ;)

  4. #4
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    it works for any number of n just it grows at n^2 so for each element you add the time growth is proportional to n^2.
    My IP address is 127.0.0.1

Similar Threads

  1. Bubble Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 08:42 PM
  2. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 06:27 PM
  3. how to sort
    By Feng in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 07:56 AM
  4. Help with sort algorithm
    By zoe in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:09 AM
  5. Insertion sort algorithm
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 06-28-2007, 09:26 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
  •