Bubble Sort
Bubble Sort
Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by
repeatedly swapping the adjacent elements if they are in the
Bubble Sort wrong order.
1 2
In Bubble Sort algorithm, Let us understand the working of bubble sort with the help
of the following illustration:
• traverse from left and compare adjacent elements and the
higher one is placed at right side. Input: arr[] = {6, 3, 0, 5}
3 4
12/7/2023
How does Bubble Sort Work? How does Bubble Sort Work?
Second Pass: Place the second largest element at correct
First Pass: The largest element is placed in its correct position.
position, i.e., the end of the array.
5 6
How does Bubble Sort Work? How does Bubble Sort Work?
Third Pass: Place the remaining two elements at their correct
positions. Total no. of passes: n-1
7 8
12/7/2023
9 10
Bubble sort is a simple sorting algorithm. It is not suitable for large data sets because of its time complexity.
Advantages:
It works by repeatedly swapping adjacent elements if they are in
Bubble sort is easy to understand and implement.
the wrong order.
It requires minimal additional memory space to perform the
The algorithm sorts the array in ascending or descending order. sorting.
Disadvantages:
It has a time complexity of O(n2) in the worst case, where n is the It is not efficient for large data sets because of its time
size of the array. complexity.
It has poor performance compared to other sorting algorithms,
such as quicksort and mergesort.
Sumit Srivastava @ BIT Mesra Sumit Srivastava @ BIT Mesra
11 12