Saturday, December 15, 2012
Bubble sort in Java - program to sort integer array
Bubble sort is one of the classic sorting algorithm which is used to explain sorting during various computer and engineering courses. Because of its algorithmic nature and simplicity its often used in various Java and C++ programming exercises. You may expect questions like Write Java program to sort integer array using bubble sort during any programming interview. Since algorithmic questions are always tricky question and not easy to code. Even simplest of them can lead to confusion, especially if you are not gifted with a natural programming head. I have seen many developers fumble if asked to code on the spot. That's why its advisable to do algorithmic and logical programming during training and learning programming and OOPS to get this skill of converting logic into code. Let's come back to Bubble sort, In Bubble sort algorithm we sort an unsorted array by starting from first element and comparing with adjacent element. If former is greater than later then we swap and by doing this we get the largest number at the end after first iteration. So in order to sort n elements you require n-1 iteration and almost n-1 comparison. For recap here is the logic for bubble sort sorting algorithm :
1) start comparing a to a
2) if a > a then swap numbers e.g. a=a and a=a
3) compare a to a and repeat till you compare last pair
4) This is referred as one pass and at the end of first pass largest number is at last
5) repeat this comparison again starting from a but this time going till second last pair only
Now let's see Java program which implements this bubble sort logic to sort unsorted integer array.
Here is complete code example of bubble sort in Java. It uses same algorithm as explained in first pass, it uses two loops. Inner loop is used to compare adjacent elements and outer loop is used to perform Iteration. because of using two loops it result in order of n^2 which is not great in terms of performance. If you are using Array List instead of array than you can sort them using Collections.sort method for better performance, for details check How to sort Array List in ascending and descending order.
That's all on How to sort integer array using Bubble sort in Java. We have seen a complete Java program for bubble sort and also printed output after each pass or iteration, if you look at carefully you will find that after each pass largest number gets sorted and number of comparison decreased. As I said Bubble sort is not a high performance sorting algorithm and you should by using Collection.sort() method from standard Java library to sort Collections or Arrays.sort() to sort Array in Java. Also this program demonstrate How to print contents of Array using Arrays.toString() as array in Java doesn’t override toString and simply printing array using System.out.println(array) will only show defulat toString from java.lang.Object class instead of contents of array.
Other programming tutorials from java67 you may find useful