Learn Before
Concept
Logic behind Bubble Sort
This algorithm is the simplest of all sorting algorithms. It works by repeatedly swapping the adjacent elements if they are in the wrong order. The name originates from how the elements "bubble" or "float" up to its correct place.
For instance, to order the array (5, 1 , 4, 2, 8) in ascending order, it will take the following steps: Loop #1
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ) ( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ) ( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ) ( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
Loop #2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ) ( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Loop #3
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
0
1
Updated 2021-07-20
Tags
Python Programming Language
Data Science