How to implement Bubble Sort in Python?

Related Courses

Introduction:

  1. As we know that the Python is more powerful language which offer great tools for data crunching and preparation, as well as for complex scientific data analysis and modelling. 

  2. Python has multiple implementations including Jython, scripted in Java language for Java Virtual Machine.

  3. Most of the Python modules work on community development model and are open-source and free.

  4. When we are going to use the python for programming approach, then there are several things we need to know.

  5. One of the important techniques is Sorting. In Sorting we are going to arrange the data in an increasing or decreasing order according to some linear relationship among the elements.

Here I am going to discuss about the working approach of Bubble sort. I am also going to discuss the step wise discussion as below.

What is Bubble sort?   

  1. The Bubble sort is the most common techniques which is being used for arranging the elements in either increasing or decreasing order. It is also known as sinking sort. 

  2. In this technique the list elements to be get sorted, are used to comparing each pair of adjacent items.

  3. During the comparison depending on the condition specified they are allowed to swap if they are not in the correct order.

  4. The above steps mentioned are repeated until no more swaps are needed. which is used to yield the final sorted list.

Steps for performing a Bubble Sort:

During the process of bubble sort, the following steps are used to arrange the elements. These steps are mentioned as below.

  1. In the beginning of the process, we need to pick the first and second element in the list compare and allow to get swap depending upon the condition specified if they are not in the correct order.

  2. The same processes of comparing to be done for second and third element and swap them if they are in the wrong order.

  3. Proceed similarly until the last element of the list in a similar fashion.

  4. Keep repeating all of the above steps until the list is sorted.

Bubble Sort Algorithm:

As we have already discussed above that the Bubble Sort is a sorting algorithm technique which is basically used to sort the list items in either ascending or descending order. If we need to arrange the elements of the list in ascending order then it can be done by comparing two adjacent values. 

During the process If we are going for ascending order then if the first value is higher than second value, then swapping will took place and the first value takes the second value position. Otherwise swapping will not takes place if the first element is lesser than the second value.

The basic algorithm used for such case is discussed below.

Step 1) At the first we need to get the total number of elements. Get the total number of items in the given list.

Step 2) We need to determine the number of outer passes (n - 1) to be done. If the number of elements in the list is n then it is the n-1.

Step 3) Here we need to perform the inner passes (n - 1) times for outer pass 1. During this process the first element is get compare with the second element and If the second value is less than the first value, then swap the positions.

Step 4) Repeat step 3 until we reach to the outer pass (n - 1). 

To understand the mechanism in better manner let us consider the following example as below.

Example:

Let us consider the list as 

A=[21,6,9,33,3]

Step-1: The values 21 and 6 are compared to check which one is greater than the other.

[21,6,9,33,3]

Since, the 21 is greater than 6, so 21 takes the position occupied by 6 while 6 takes the position that was occupied by 21. So now the new list will be 

[6, 21,9,33,3]

Step-2: The values 21 and 9 are compared.

[6, 21,9,33,3]

Here since, 21 is greater than 9, so we swap the positions of 21 and 9. So the new list will be 

[6,9, 21,33,3]

Step-3: The values 21 and 33 are compared to find the greater one.

 [6,9, 21,33,3]

Here since, The value 33 is greater than 21, so no swapping takes place.

Step-4: The values 33 and 3 are compared to find the greater one.

[6,9, 21,33,3]

Here The value 33 is greater than 3, so we swap their positions. So the new list is 

[6,9, 21,3,33]

The sorted list at the end of the first iteration is like the one above, But during the second iteration the new list is as follows

[6,9, 3,21,33]

After the third iteration the new list is as follows

[6,3,9,21,33]

After the forth iteration the new list is as follows

[3,6,9,21,33]

The above is the final sorted list.

Python Program to implement Bubble Sort:

def bubbleSort( theSeq ):
    n = len( theSeq )
    for i in range( n - 1 ) :
        flag = 0
for j in range(n - 1) :
            if theSeq[j] > theSeq[j + 1] : 
               tmp = theSeq[j]
                theSeq[j] = theSeq[j + 1]
               theSeq[j + 1] = tmp
flag = 1
        if flag == 0:
break
    return theSeq
el = [21,6,9,33,3] 
result = bubbleSort(el)
print (result)

In the above code, we compare the adjacent numbers and swap them if they are not in the correct order. 

Scope @ NareshIT:

  1. At Naresh IT you will get a good Experienced faculty who will guide you, mentor you and nurture you to achieve your dream goal.

  2. Here you will get a good hand on practice in terms of practical industry-oriented environment which will definitely help you a lot to shape your future.

  3. During the designing process of application, we will let you know about the other aspect of the application too. 

  4. Our Expert trainer will let you know about every in’s and out’s about the problem scenario.

Achieving your dream goal is our motto. Our excellent team is working restlessly for our students to click their target. So, believe on us and our advice, and we assured you about your sure success.