Home > Problem, Projects/Experiments, Python > Python: Sorting Algoritm – Bubble Sort

Python: Sorting Algoritm – Bubble Sort


Learning : Python, Math, Algorithm
Subject: Sorting Algoritm – Bubble Sort

[NOTE: To keep the code as simple as we can, We WILL NOT ADD any user input Varevecations. Assuming that our user will Enter the right inputs.]

Sorting Algorithm is a way to sort a given list of numbers, there are several sorting Algorithm as follow:
Type of Sorting Algorithm
Quick Sort. [Click to Read]
Bubble Sort.
Merge Sort. [Commeing Soon]
Insertion Sort. [Commeing Soon]
Selection Sort. [Commeing Soon]
Heap Sort. [Commeing Soon]
Radix Sort. [Commeing Soon]
Bucket Sort. [Commeing Soon]

Here in this post we will write a function to take a given list and sort it then pass it back. We assume the user will enter a serial of numbers, that he want to sort, our function will sort it and print out the original numbers and the sorted one.

Bubble Sort: Steps of Bubble Sorting Algorithm are:
1. We will select the First Number (Current number) in the list and Compare it with the next number in the list. (Compare list[0] and list[1])

2. If the Next Number in the list IS SMALLER than Current Number SWAP the Two, then compare the Current with the third number and so on.

2.2 If the Next Number in the list IS NOT SMALLER than Current Number, Then we LEFT the Current Number in it’s Position and will Set the Next number as the Current. And will start Comparing Again as Step 2 until the last number in the list.

3. When we reach the end of the list and we perform a SWAP, then we start again with First Element as STEP 1.

4. If we reach the end of the list Without any SWAP being made, then the list is ordered and the algorithm can stop.

Coding Starting with the Main Menu Function and a Function to ask the
User to enter the numbers in the list, here are those two Functions.

python code bubble sorting algorithm project by Ali Radwani doha qatar



And here is the Main code that will call the Menu and trigger the Functions based on the User selecting.

python code bubble sorting algorithm project by Ali Radwani doha qatar



Also we have the Function that will ask the user to Enter the Numbers for the List or Array to be sort.
All the code will be Available as a .py file in the Download Page [Click Here]

Now let’s see the code for Bubble Sorting Algorithm, we will have two version of the code, one for Fast-Run that takes the list and return it back as Sorted, and another one will be to show the Details Step By Step of applying the Algorithm. Both Functions are the same but the details one will have several print statements to show the steps, here I am posting the Fast-Run version.


Run Screen..




End of Bubble Sort Algorithm, hope you enjoy it.


.. Have Fun With Coding ..

To Download my Python code (.py) files Click-Here


Follow me on Twitter..

By: Ali Radwani

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s