Beruflich Dokumente
Kultur Dokumente
• Bubble sort is an algorithm that compares the adjacent elements and swap their
positions if they are not in the intended order. The order can be ascending or
descending.
• It is known as bubble sort, because with every complete iteration the largest element
in the given array, bubbles up towards the last place or the highest index, just like a
water bubble rises to the water surface.
• To traverse through every element we use Passes.
Explanation with Example
42 33 23 74 44
We have to sort the following array in ascending order using bubble sorting.
Explanation with Example
Pass-1: Compare A[N]with A[N+1] and arrange them so that A[N] < A[N+1]
42 33 23 74 44
33 42 23 74 44
33 23 42 74 44
33 23 42 74 44
33 23 42 44 74
Explanation with Example
33 23 42 44 74
23 33 42 44 74
23 33 42 44 74
23 33 42 44 74
Explanation with Example
Pass-3: Repeat Pass 2 with one less comparisons
23 33 42 44 74
23 33 42 44 74
23 33 42 44 74
Explanation with Example
23 33 42 44 74
23 33 42 44 74
Number of Passes required.
The number of interchanges depend upon the data as well as on the total
number of elements (N).
The total number of interchanges is always less than or equals to the total
number of comparisons.
The total number of interchanges = Sum of all interchanges in all passes.
In the above example we have total 5 element, so the total interchanges will
be
Total Interchanges = 4 + 1 = 5.
References
• https://www.quora.com/What-is-the-total-number-of-comparisons-in-a-bubbl
e-sort
• Data Structures With C – by Schaum Series
• https://www.programiz.com/dsa/bubble-sort
• Principle of Data Structures Using C and C++ – by Vinu V DAS
Conclusion