The Bubble Sort is the simplest Sorting Algorithm. The main concept is:
- Compare two adjacent elements
- If the first item is Bigger than the second item then swap it.
- Run the loop for n-1 items.
- Keep doing it for all the elements.
# let's try.....
A simple swaping system is given below
# remember, a simple 'swaping' system is needed before makin Bubble_sort And Selection_Sort Algorithm.
# it'll make your task easier, Trust me it'll be very helpful 😎😎
a = 5
b = 2
print(a, b)
tem = a
a = b
b = tem
print(a, b)
Let's see Bubble Sorting Algorithm
# inner 'for' loop, it will continuously Compare two adjacent Elements and swap them ....
for i in range(len(array) - 1):
if array[i] > array[i + 1]:
temp = array[i]
array[i] = array[i + 1]
array[i + 1] = temp
# print(array)
# now, time to loop for each single 'elements', so, let's dump the 'first or inner 'for' loop' into a outter 'for' loop ,
# the outter for loop will loop the inner for loop for each element and finally we'll get the 'sorted list'
for j in range(len(array)):
for i in range(len(array) - 1):
temporay = array[i]
array[i] = array[i + 1]
array[i + 1] = temporay
print(array)
# aaaaaaaannnnnnnnnnnnnnnndd that's all, we got the sorted list........ 😎
0 Comments