This post originated from an RSS feed registered with Java Buzz
by Javin Paul.
Original Post: Bubble sort in Java - program to sort integer array
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
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 :