Bubble Sort: Understand it better

Bubble Sort: Understand it better

Bubble sort is a simple sorting algorithm that works by repeatedly stepping through a list of elements to be sorted, comparing adjacent elements, and swapping them if they are in the wrong order.

It is called "bubble sort" because smaller elements "bubble" to the top of the list, similar to bubbles rising to the surface of water

Suppose we have the following list of athletes with?their respective running times:

Athletes:

1.John - 10.5 seconds

2.Sarah - 9.8 seconds

3.Mark - 11.2 seconds

4.Lisa - 9.2 seconds

5.Alex - 10.1 seconds

Using bubble sort, we'll sort the athletes based?on their running times:

Pass 1:

?John (10.5) > Sarah (9.8) – No swap

?Sarah (9.8) > Mark (11.2) – Swap

?Sarah (9.8) > Lisa (9.2) - No swap

?Lisa (9.2) > Alex (10.1) - Swap


Pass 2:

John (10.5) > Mark(11.2) – Swap

?John (10.5) > Sarah (9.8) –No Swap

?Sarah (9.8) > Alex (10.1) –Swap

Sarah (9.8) > Lisa (9.2) – No Swap


Follow Coding Lite to know more:

https://youtu.be/CRUJnmvQawM

要查看或添加评论,请登录

社区洞察

其他会员也浏览了