Thread: Java bubble sort

  1. #1
    Registered User
    Join Date
    Aug 2005

    Java bubble sort

    First off excuse that this isn't C++, but its super simple that i think anyone can help me ...just pretend its pseudocode

    My question is, which is better? (and of course, is the 2nd code posted even considered bubble sort)


    Heres standard bubble sort: It just goes through and makes the unsorted portion smaller each pass.
    1rst code:
    public static void bubbleSort(int[] arr) {
    		for(int i = arr.length - 1; i >= 1; i--){
    			for(int j = 0; j < i; j++)
    				if(arr[j] > arr[j+1])  {
    					int temp = arr[j];
    					arr[j] = arr[j+1];
    					arr[j+1] = temp;
    2nd code: (it keeps the unsorted portion the same size every pass but it stops 1 pass after it is sorted, while the first code continues till its done)
    public static void sort(int[] arr)
    int temp;
    boolean ordered=false;
    for(int i=0;i<arr.length-1;++i)

    (I was also thinking that it depends on the dataset you receive... is there any why to "measure" how "bad" a dataset is?)
    Last edited by rodrigorules; 01-15-2010 at 02:45 AM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Java code belongs in the Tech Forum.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Sep 2006
    The basic bubblesort just continues, but of course, people notice these inefficiencies and next thing you know you have bubblesorts with varying degrees of optimizations.

    They're still in the same group in terms of their complexity, AFAIK, but obviously the more optimized one's are also quicker than the most basic version.

    For a laugh though, you have to check out BOZO sort - leave room around your work area so you can roll on the floor, laughing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using bubble sort to sort a matrix by sum..
    By transgalactic2 in forum C Programming
    Replies: 22
    Last Post: 12-23-2008, 12:03 AM
  2. Bubble Sort Query
    By coolboarderguy in forum C Programming
    Replies: 2
    Last Post: 04-15-2008, 12:50 AM
  3. How do I bubble sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2008, 02:30 AM
  4. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM
  5. Urgent - Bubble Sort problem
    By Bada Bing in forum C Programming
    Replies: 1
    Last Post: 11-15-2001, 05:56 AM