Bubble sort algorithm – C program – Sorting algorithms

Bubble sort is the simplest but slowest sorting algorithm which we will see using c program. Lets see am example

Lets say we have numbers in array as  6, 4,9, 2

Step 1 :  We will arrange 6 and 4 in ascending order hence list become

List after step 1: 4,6,9,2

Step 2 No we will arrange 6 and 9 in ascending order and continue

List after step 2: 4,6,9,2

Hence at iteration or finishing of all arrangements till end list will be like 4,6,2,9. What bubble sort has done is put biggest number at the end. Now we do all this all over again and hence in total 4 times arrange 4 numbers to completely sort the list to 2,4,6,9.

Basic program for bubble sort is like

for(int x=0; x<n; x++)
        for(int y=0; y<n-1; y++)
			int temp = array[y+1];
			array[y+1] = array[y];
			array[y] = temp;

Bubble sort though easy take lot of time to solve problem of size n

Worst case scenario : O(n*n)

Best Case scenario : O(n) For this to happen we have to measure how many arrangements happened. If 0 then numbers are already sorted.

For large numbers it is kind of unsuitable sorting algorithm.



Bubble sort algorithm,C program

One thought on “Bubble sort algorithm – C program – Sorting algorithms

Leave a Reply

Your email address will not be published. Required fields are marked *