Bubble Sort Algorithm, Code, Advantages

By | November 24, 2023

Bubble Sort Algorithm

“The Bubble Sort Algorithm is a simple sorting algorithm used to sort an unordered set of data. Read the complete article to get a better understanding.”

Bubble Sort Algorithm: In this article, we will learn more about the Bubble sort algorithm. Bubble sort is a simple searching algorithm that repeatedly swaps adjacent elements to arrange them in sorted order, either in decreasing or increasing order. It is suitable for small or medium data sets. The average and worst-case complexity of the bubble sort algorithm is O(n2), where n is the number of items in the list.

Let us learn the code for the C program for selection sort. You can start your career with PW Skills Complete Decode Full Stack Web Dev 1.0 to boost your career and give you a strong foundation. The complete course offers recorded and live lectures and any-time doubt-solving. 

What is the Bubble Sort Algorithm?

Bubble Sort is a linear sorting algorithm used to sort data items by repeatedly swapping the items until they are not in the sorted order. It is known as bubble sorting because the sorting takes place just like moving bubbles in water. Similarly, as the bubbles rise to the surface at the end, the bubble element moves to the end in each iteration.

Bubble Sort Algorithm

Let us consider an array ‘arr’ consisting of n elements inside. We will use the bubble sort algorithm to swap the values one by one until they get sorted.

Bubble Sort Algorithm
1. bubbleSort(arr)

2. for all elements in the array

3. If arr[i] > arr[i+1]

4. Swap (arr[i], arr[i+1])

5. end if

6. end for 

7. return arr

8. end bubbleSort

Working of Bubble Sort Algorithm

Let us understand bubble sort by using an example. Consider an array consisting of five elements in an unorganized order. arr = { 16, 7, 25, 9, 2}, we will now use the bubble sort algorithm to sort the array element.

1. First Pass

In the first pass, the algorithm will select the first two elements using the swap function. It will compare them and if element at arr[i-1] is greater than element at arr[i], then swap both the element.

First Step for Bubble Algorithm
First Step for Bubble Algorithm

Now, after swapping, we will move on to the next element. But at the next element at arr[i-1] is not greater than element at arr[i], then we do not swap both the elements and move to the next element.

First Step Bubble Sort Algorithm 2
First Step Bubble Sort Algorithm 1.1

Now, we can see the element at arr[2] is greater than element at arr[3], hence we will swap.

First Step Bubble Sort Algorithm 1.2
First Step Bubble Sort Algorithm 1.2

Now, we will compare the last two elements. The elements at arr[3] are greater than arr[4]. Hence, we swap again.

First Step Bubble Sort Algorithm 1.3
First Step Bubble Sort Algorithm 1.3

Now, after the first pass, the elements arranged are given below.

First Step Bubble Sort Algorithm 1.5
First Step Bubble Sort Algorithm

2. Second Pass

Now, we will start again in the second pass from the element at arr[0] and element at arr[1]. Now, we see the element at 0th index is smaller than element at the first index, hence we do not swap.

Second Pass Bubble Sort Algorithm
Second Pass Bubble Sort Algorithm

Now, we check that at arr[1] the element is greater than element at arr[2], hence we swap the element.

Second Pass Bubble Sort Algorithm 1.1

Now, we again swap the elements at arr[2] and arr[3] indexes. 

Second Pass Bubble Sort Algorithm 1.2

Now, we check the element at arr[3] is not greater than element at arr[4]. Hence, we will not swap the element. Hence, the elements after the second pass will look like this.

Second Pass Bubble Sort Algorithm 1.3

3. Third Pass

Now, at the third pass the elements in the array looks like this.

Third Pass Bubble Sort Algorithm

Elements at arr[0] are smaller than elements at arr[1], hence we will not swap the elements. Now, we will swap the elements at the next two positions.

Third Pass Bubble Sort Algorithm 1.1

Now, after the element at arr[2] we can see the elements are in correct order. Hence, the element order after the third pass will look like this.

Third Pass Bubble Sort Algorithm 1.2

4. Fourth Pass

Now, our array elements look like this in the fourth pass. We can see the elements at arr[0] index is greater than element at arr[1], hence we will swap both of them.

Forth Pass Bubble Sort Algorithm 1.0

Now, our array is sorted, all the elements are its correct position. It takes (N-1) pass to completely sort the elements of array.

2 7 9 16 25

Forth Pass Bubble Sort Algorithm 1.1

Advantages of Bubble Sort Algorithm

Microservices in Java
Microservices in Java

The Bubble sort algorithm is one of the sorting algorithms used to sort the array items in either ascending or descending order.

  • It is simple algorithm easy to understand and implement.
  • It is beginner friendly algorithm and can be good for explanatory purposes.
  • The space complexity for bubble sort algorithm is constant, O(1). No additional data or memory is required, it is an inplace sorting algorithm.
  • It can be adapted easily.
  • It is a stable algorithm, which means that it preserves the relative order of equal elements found in the array.

Disadvantages of Bubble Sort Algorithm

Despite being a simple and easy to implement sorting algorithm, bubble sort has some disadvantages, as given below.

  • The time complexity of the bubble sort algorithm is quadratic, O(n2), where N is the size of array.
  • It is not suitable for large datasets. As the element in array increases the performance of bubble sort decreases quardatically.
  • It is very less used in the practical world.

Complexity Analysis of Bubble Sort Algorithm

The complexity analysis of bubble sort is given in the table below.

Bubble Sort Complexity Analysis
Complexity Type Selection Sort
Time Complexity O(n^2)
Space Complexity O(1)

Bubble Sort Algorithm FAQs

What is Bubble Sort algorithm?

Bubble Sort is a linear sorting algorithm used to sort data items by repeatedly swapping the items until they are not in the sorted order.

What is the time complexity of the Bubble sort algorithm?

The time complexity of the bubble sort algorithm is quadratic which is O(n^2).

How many passes does it take to complete the sorting using bubble sort?

It takes N-1 passes to sorting using bubble sort algorithm. The N here is the size of the element.

Is bubble sort a stable algorithm?

Yes, bubble sort is a stable algorithm. It preserves the relative order of elements equal in the array.

Is bubble sort suitable for large datasets?

No, bubble sort is suitable for smaller datasets only. However, it is not generally used in the real world.

Telegram Group Join Now
WhatsApp Channel Join Now
YouTube Channel Subscribe
Scroll to Top
close
counselling
Want to Enrol in PW Skills Courses
Connect with our experts to get a free counselling & get all your doubt cleared.