—Sorting rearranges the elements in a list into either ascending or descending order. (we’ll use ascending order.) In this article, we’ll explain the working of Bubble sort. Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.

Bubble sort compares neighboring pairs of array elements, starting with the last array element, and swaps neighbors whenever they are not in correct order. On each pass, this causes the smallest element to “bubble up” to its correct place in the array.

Bubble Sort Example

Bubble Sort: Pass One

Bubble Sort: Pass Two

Bubble Sort:  Pass Three

Bubble Sort:  Pass Four

Bubble Sort: How many comparisons?

The number of comparisons when the array contains N elements is

  Sum = (N-1)  +   (N-2)  + .  .  .  +  2  +  1

Bubble sort implementation

void bubbleSort(int arr[], int size)
	for (int i=0; i < size; i++)
		for (int j = size-1; j>i; j--)
			if(arr[j] < arr[j-1] )
				int temp = arr[j];
				arr[j] = arr[j-1];
				arr[j-1] = temp;
For further reference
—An excellent animation of all studied sorting algorithms can be seen at:
Tagged with: C/C++ languageData structures

One Response to Bubble Sort: A Graphical Explanation

  1. nilu butay says:

    your code is good.but next you cook para dili ta manga pasmo….

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.