Code in Python
Your How-to Guide

Bubble Sort in Python

23rd July 2017

This code snippet below will show you how to perform bubble sort in python. Creating a bubble sort program in python will touch the concepts in python like lists, indexing, for/while loops in python, conditional statements like if-elif-else. Follow us on Facebook, Twitter and Github to stay updated.

# ***********BUBBLE SORT**************

# taking array from user

arr = []
num = int(input("enter number of elements: "))
for j in range(num):
    arr.insert(j, int(input("enter element: ")))
print(arr)

# sorting that array

for pos in range(len(arr) - 1, 0, -1):
    for i in range(pos):
        if arr[i] > arr[i + 1]:
            temp = arr[i]
            arr[i] = arr[i + 1]
            arr[i + 1] = temp
print("sorted array is : ", arr)


About Author

Akarsh Singh

Developer, Tech. Evangalist, Loves to read and write about new tech. Madly in love with Python. Follow him on social media.

1. For more Awesome ads-free content help me to stay awake late. Buy me a coffee.

2. Want to know where Code in Python website is hosted. Buy cheapest Web hosting (less than 1.5$/month or Rs.99/month) using this referral link. This is trusted and tested web hosting service Code in Python relies on.

backupWeb hosting
3. Support Akarsh Singh(Founder, Code in Python) so that he can teach you more Python and Machine Learning Concepts for free. Contribute to his Paypal.me page and he will mention you in his next post.

favoriteAkarsh
4. Indian Users can donate through Paytm.