Data Science and Machine Learning Internship ...
- 22k Enrolled Learners
- Weekend/Weekday
- Live Class
Sorting means arranging any data in an increasing or decreasing order according to some linear relationship among the elements. This article on Bubble Sort in Python will help you understand this concept in detail.
We’ll be covering the below topics in this blog:
Bubble sort is also known as sinking sort. It is a simple sorting algorithm that continuously steps through the list to be sorted, comparing each pair of adjacent items and swapping them if they are not in the correct order. The steps are repeated until no more swaps are needed, which is when the list is sorted.
The above steps will be more clear by the following visualizations –
Now let us look at the Algorithm behind Bubble Sort.
First Pass:
( 16,19,11,15,10 ) –> ( 16,19,11,15,10 ) – The algorithm compares first two elements and swaps since 19 > 16
( 16,19,11,15,10 ) –> ( 16,11,19,15,10 ) – Swap since 19 > 11
( 16,11,19,15,10 ) –> ( 16,11,15,19,10 ) – Swap since 19 > 15
( 16,11,15,19,10 ) –> ( 16,11,15,10,19 ) – Now, since these elements are already in the correct order (19 > 10), the algorithm does not swap them.
Second Pass:
( 16,11,15,10,19 ) –> ( 11,16,15,10,19) – Swap since 16>11
( 11,16,15,10,19 ) –> ( 11,15,16,10,19 ) – Swap since 16 > 15
( 11,15,16,10,19 ) –> ( 11,15,10,16,19 ) – Swap since 16>10
( 11,15,10,16,19 ) –> ( 11,15,10,16,19 )
The array is sorted, but our algo does not know if it is completed. Hence, it needs another whole pass without any swap to know it is sorted.
Third Pass:
( 11,15,10,16,19 ) –> ( 11,15,10,16,19 )
( 11,15,10,16,19 ) –> ( 11,10,15,16,19) – Swap since 15>10
( 11,10,15,16,19 ) –> ( 11,10,15,16,19 )
( 11,10,15,16,19 ) –> ( 11,10,15,16,19 )
( 11,10,15,16,19 ) –> ( 10,11,15,16,19) – Swap since 11>10
The final output is ( 10,11,15,16,19)
Let us now code this up –
Python Program to implement Bubble Sort
a = [16, 19, 11, 15, 10, 12, 14]
#repeating loop len(a)(number of elements) number of times for j in range(len(a)): #initially swapped is false swapped = False i = 0 while i<len(a)-1: #comparing the adjacent elements if a[i]>a[i+1]: #swapping a[i],a[i+1] = a[i+1],a[i] #Changing the value of swapped swapped = True i = i+1 #if swapped is false then the list is sorted #we can stop the loop if swapped == False: break print (a)
OUTPUT:
In the above code, we compare the adjacent numbers and swap them if they are not in the correct order. Repeat the same process len(a) number of times. We have assigned a variable ‘swapped’ and made it ‘True’ if any two elements are swapped in an iteration. And if there is no interchanging of elements then the list is already sorted and hence, there is no change in the value of the ‘swapped’ and we can break the loop.
With this, we come to an end of the blog titled “How to implement Bubble Sort in Python”. I hope the content added value to your Python knowledge.
Got a question for us? Please mention it in the comments section of this “How to implement Bubble Sort in Python” blog and we will get back to you as soon as possible.
To get in-depth knowledge on Python along with its various applications, you can enroll for live Python online training with 24/7 support and lifetime access.
Course Name | Date | Details |
---|---|---|
Data Science with Python Certification Course | Class Starts on 19th October,2024 19th October SAT&SUN (Weekend Batch) | View Details |
Data Science with Python Certification Course | Class Starts on 14th December,2024 14th December SAT&SUN (Weekend Batch) | View Details |
edureka.co