Results 1 to 2 of 2
- 05-02-2012, 01:57 PM #1
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0
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
- 05-02-2012, 03:36 PM #2
Re: Help with Bidirectional Bubble Sort
Best way to improve the performance of bubble sort? Don't use bubble sort.
Recommended reading: Bubble sort - Wikipedia, the free encyclopediaHow 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
-
Bubble Sort objects
By dougie1809 in forum New To JavaReplies: 23Last Post: 03-26-2012, 12:19 AM -
Bubble sort
By pineapple in forum New To JavaReplies: 3Last Post: 04-25-2009, 12:45 AM -
How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04-29-2008, 08:04 PM -
Bubble Sort in Java
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-15-2008, 07:42 PM -
need help with bubble sort
By lowpro in forum New To JavaReplies: 3Last Post: 12-17-2007, 05:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks