Bubble Sort in Data Structures
Heyy !!
If you are new to the Data structures & algorithms then you will feel easy to understand by this article.
Basically, Bubble Sort is a Sorting technique in Data Structures to sort the given numbers
- Simple & Most Popular
- Based on Successive Selection of smallest element through the exchange of element
Logic :
- Suppose n be the number of elements present in the array
- So first pass (i.e. Pass1) comparison starts in between a[0] & a[1]
- In that, if a[0] is larger than a[1] then they are exchanged
- Now, the larger element is present at a[1]
- This process goes on repeating till the last element
- After the compilation of 1st pass, a larger element is placed at the last position
This sort requires n-1 passes.
Example :
=> We have 5 9 6 2 8 1
Pass1
5 9 6 2 8 1
5 9 6 2 8 1
5 6 9 2 8 1
5 6 2 9 8 1
5 6 2 8 9 1
5 6 2 8 1 9
Pass2
5 6 2 8 1 9
5 6 2 8 1 9
5 2 6 8 1 9
5 2 6 8 1 9
5 2 6 1 8 9
5 2 6 1 8 9
Pass3
5 2 6 1 8 9
2 5 6 1 8 9
2 5 6 1 8 9
2 5 1 6 8 9
Pass4
2 5 1 6 8 9
2 5 1 6 8 9
2 1 5 6 8 9
Pass5
2 1 5 6 8 9
1 2 5 6 8 9
Now We have Sorted Array List → 1 2 5 6 8 9
( Our array size was 6 and this sort requires n-1 passes so our passes are 5 and after that 5 passes we got our sorted list of the array )