Member-only story

Bubble Sort

Pratik Rai
4 min readAug 10, 2024

--

Bubble Sort

Credits: TeaMochi

It is a sorting algorithm which works by repeatedly swapping the adjacent elements if they are in wrong order or not in the required order.

> It gets its name from the way the smaller element bubbles or moves to the top or the first position.
> The element keeps getting swapped with the adjacent element until they are in the correct order.> It works for certain rounds everytime from the first position to get the fully sorted array at the end.

Example to understand the complete process👇🏻

Suppose we have an array of numbers:

[4,2,8,1,7]

Now for the array given above these will be the involved rounds of bubble sorting :

<ins>Round-1</ins>

  • Firstly it will compare 4 and 2 and swapping will be there as 4 is greater than 2.
  • [4,2,8,1,7] -> [2,4,8,1,7]
  • Now the second and third element i.e. 4 and 8 will be compared and no swapping will be there as 4 is smaller than 8.
  • [2,4,8,1,7] -> [2,4,8,1,7]
  • Now the next two elements i.e. 8 and 1 will be compared and both the elements will be swapped as 8 is greater than 1.
  • [2,4,8,1,7] -> [2,4,1,8,7]
  • Now the last two elements will be compared i.e. 8 and 7 and it is clear that…

--

--

No responses yet