Mastering Python (97 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (32 Blogs)
  • Data Science (26 Blogs)
  • Mastering Python (92 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

How to implement Bubble Sort in Python?

Last updated on Sep 19,2019 4.8K Views

9 / 11 Blog from Python Programs

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:

What is Bubble Sort?

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.

Steps for performing a Bubble Sort

  • Compare first and second element in the list and swap if they are in the wrong order.
  • Compare second and third element and swap them if they are in the wrong order.
  • Proceed similarly until the last element of the list in a similar fashion.
  • Keep repeating all of the above steps until the list is sorted.

The above steps will be more clear by the following visualizations –

Bubble sort in Python - Edureka

Swap-Python Bubble sort - EdurekaSecond iteration-Bubble sort in Python - Edureka

Third iteration-Bubble sort- Edureka

Bubble Sort Algorithm

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 )

Fourth Pass:

( 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: 

Output - Edureka
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.

Make sure you practice as much as possible and revert your experience.

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.

Upcoming Batches For Data Science with Python Certification Course
Course NameDate
Data Science with Python Certification Course

Class Starts on 13th February,2023

13th February

MON-FRI (Weekday Batch)
View Details
Data Science with Python Certification Course

Class Starts on 25th February,2023

25th February

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How to implement Bubble Sort in Python?

edureka.co