How does Bubble Sort Work?
Let us understand the working of bubble sort with the help of the following illustration:
Input: arr[] = {6, 0, 3, 5}
First Pass:
The largest element is placed in its correct position, i.e., the end of the array.
Second Pass:
Place the second largest element at correct position
Third Pass:
Place the remaining two elements at their correct positions.
- Total no. of passes: n-1
- Total no. of comparisons: n*(n-1)/2
Bubble Sort – Data Structure and Algorithm Tutorials
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity is quite high.
Contact Us