/* Ajith - Syntax Higlighter - End ----------------------------------------------- */
Showing posts with label Bubble Sorting. Show all posts
Showing posts with label Bubble Sorting. Show all posts

11.04.2013

Bubble Sorting

Bubble sort is one of the simple sorting algorithms and also popularly known as a Brute Force Approach. The logic of the algorithm is very simple as it works by repeatedly iterating through a list of elements, comparing two elements at a time and swapping them if necessary until all the elements are swapped to an order.

For e.g. if we have a list of 10 elements, bubble sort starts by comparing the first two elements in the list. If the second element is smaller than the first element then it exchanges them. Then it compares the current second element with the third element in the list. This continues until the second last and the last element is compared which completes one iteration through the list. By the time it completes the first iteration the largest element in the list comes to the rightmost position.

Image courtesy from Annieink.com

The algorithm gets its name as we start from lowest point and “bubble up” the higher elements to the highest point in the list. We can also follow other approach where we start from highest point and “bubble down” lowest elements in the list. Since it only uses comparisons to operate on elements, it is a comparison sort.