Bubble Sort in Python:
Bubble sort is a sorting technique used in python programming language to arrange the sequence in ascending or descending order. The basic principle of bubble sort is to check and compare two adjoining values of sequence and swap if not in definite order.This method takes many operations to sort all elements of a sequence.
Suppose we have a list of unsorted values. let's say a list i.e,[17,55,43,12,45,77 ] and we want to sort this list in ascending order.
We will be comparing first two values and swap if first element greater than second , checking the values second with third, third with forth and so on. Swapping here means interchanging the values.
Of course, we will be using loop here. But if we use only one loop for doing all the operations, it will not sort all the elements of a list. Because if we perform only one iteration, only the heavier element will place at bottom of list, but other values remain unsorted. For arranging all other values in a definite order, we have to use outer- inner concept here. So that if in one iteration, largest value settle at bottom position we can compare remaining elements by the logic (0 to n-1)( 0 to n-2) and so on(here n is the length of the list). So that if heavier element place at bottom at first loop, we will not be taking that value in second loop and so on.
Code for sorting above list:
Sorted list:
Working of bubble Sort:
Elements inside black box are sorted in the list. We would use 4 iterations for list of 5 length because we know if in 4 iterations 4 elements are settled, then ofcourse the remaining one will be sorted.
Bubble sort algorithm in python.
Reviewed by Nikita kulyal
on
February 02, 2020
Rating:

Though I am a c++ aspirant. I have no idea about python. But this was very useful.
ReplyDeleteThank u.it really helped.
ReplyDeleteThanku for the Information 😊
ReplyDeleteI was not knowing about bubble short program.It really helped me a lot.Thanks for the useful information.Thanks a lot nikita kulyal
ReplyDeleteThanks for sharing this blog. This article gives lot of information.
ReplyDeletePython Training in Hyderabad
Python Training
Python Online Training