Bubble Sort in Data Structures

Bhargav Joshi
2 min readJun 19, 2021

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 :

  1. Suppose n be the number of elements present in the array
  2. So first pass (i.e. Pass1) comparison starts in between a[0] & a[1]
  3. In that, if a[0] is larger than a[1] then they are exchanged
  4. Now, the larger element is present at a[1]
  5. This process goes on repeating till the last element
  6. 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 )

I hope you find this useful, so please share as much as you can. Subscribe to my newsletters so you won’t miss any articles as well as do clap. Thanks !!

Join me on Social Media → LinkedIn , YouTube , Instagram , Telegram

--

--