Results 1 to 2 of 2
  1. #1
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Help with Bidirectional Bubble Sort

    Hi,

    I am trying to sort the elements in the array using bidirectional bubble sort; though i am able to get the correct answers; I want your reviews on improving the code performance:-

    Java Code:
    public class BiBBubbleSortImproved {
    	public static void main(String[] args) {
    		int[] arr = { 9, 12, 4, 99, 120, 1, 3, 10,14,-1 };
    		System.out.print("Before Sort:-");
    		for (int bsort = 0; bsort < arr.length; bsort++) {
    			System.out.print(arr[bsort] + "\t");
    		}
    		biDirBubbleSort(arr, arr.length);
    		System.out.println("Performing the Bidirectional Bubble Sort now");
    		System.out.println("After Sort:-");
    		for (int asort = 0; asort < arr.length; asort++) {
    			System.out.print(arr[asort] + "\t");
    		}
    
    	}
    
    	public static void biDirBubbleSort(int[] a, int len) {
    		System.out.println("\nThe array has " + len + " elements");
    
    		for (int index = 0; index < (a.length / 2) + 1; index++) {
    			int f = a.length - 2;
    			int s = a.length - 1;
    
    			for (int eindex = 0; eindex < a.length - 1; eindex++) {
    				int buffer = 0;
    				if (a[eindex] > a[eindex + 1]) {
    					buffer = a[eindex];
    					a[eindex] = a[eindex + 1];
    					a[eindex + 1] = buffer;
    				}
    				if (a[f] > a[s]) {
    					buffer = a[f];
    					a[f] = a[s];
    					a[s] = buffer;
    					f = f - 1;
    					s = s - 1;
    				}
    			}
    		}
    	}
    }


    Thanks in advacne.

    BR
    Ankit

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Help with Bidirectional Bubble Sort

    Quote Originally Posted by ankiit View Post
    I want your reviews on improving the code performance
    Best way to improve the performance of bubble sort? Don't use bubble sort.

    Recommended reading: Bubble sort - Wikipedia, the free encyclopedia
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Bubble Sort objects
    By dougie1809 in forum New To Java
    Replies: 23
    Last Post: 03-26-2012, 12:19 AM
  2. Bubble sort
    By pineapple in forum New To Java
    Replies: 3
    Last Post: 04-25-2009, 12:45 AM
  3. 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
  4. Bubble Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:42 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
  •