Online Tutorials & Training Materials | STechies.com
Register Login
Advertisement

Python Program for Bubble Sort

|| 0

Python Program for Bubble Sort
Stechies

Bubble Sort, a sorting program in its simplest forms, works by making multiple passes across a list. This article will help you understand what is Bubble Sort in Python and the steps involved in implementing Bubble Sort in Python codes.

What is Bubble Sort in Python?

In the Bubble Sort program, two adjacent elements comprising of a list are first checked and then swapped. In case the adjacent elements are in the incorrect order then the process keeps on repeating until a fully sorted list is obtained. Each pass that goes through the list will place the next largest element value in its proper place. So, in effect, every item bubbles up with an intent of reaching the location wherein it rightfully belongs. The Bubble Sort sorting program gets referred to in technical interviews quite often and thus carries a lot of significance for Python aspirants.

Steps Involved in Python Bubble Sort

Bubble Sort code constantly iterates through an array of unsorted elements and swaps values within the array until no further swaps are possible within a full swap that takes place through the same array.

The steps involved in the performance of a Bubble Sort algorithm in Python are:

  1. The first and second elements in the list are compared and swapped by the sorting program in case they are in the wrong order.
  2. The second and third elements in the list are compared and swapped by the sorting program in case they are in the wrong order.
  3. The process continues until the last element present in the list is compared and swapped in the same fashion.
  4. All the steps are iterated until the entire list gets sorted.
  5. The following visualizations will make the steps of Bubble Sort in Python clearer for you

Bubble-Sort-Python

Bubble Sort Simple Program Code

arraytoSort = [16, 19, 11, 15, 10, 12, 14]
#repeating the loop for len(arraytoSort)(number of elements) times
for j in range(len(arraytoSort)):
    #initially swapFlag is returned as false
    swapFlag = False
    i = 0
    while i<len(arraytoSort)-1:
        #compare the adjacent elements
        if arraytoSort[i]>arraytoSort[i+1]:
            #swapping
            arraytoSort[i],arraytoSort[i+1] = arraytoSort[i+1],arraytoSort[i]
            #Changing the value of the swapped elements
            swapFlag = True
        i = i+1
    #if swapFlag is returned as false then the entire list is sorted
    # the loop can be stopped
    if swapFlag == False:
        break
print (arraytoSort)

Output

[10, 11, 12, 14, 15, 16]

The above bubble sort code in Python allows the user to compare adjacent elements and swap them in case they are in the incorrect order. The entire process is repeated len(arraytoSort) times, wherein len(arraytoSort) refers to the total number of elements present in the list. A variable ‘swapFlag’ has also been assigned and given the value of ‘true’ in case any two elements are successfully swapped in any iteration. If the interchanging of elements fails to take place then it means that the entire list is sorted and therefore no further changes are taking place in the value of ‘swapFlag’ variable. This is the point when the loop can be broken.

Bubble Sort Python Code Example

The following Bubble sort code depicts how the same can be used in the Python programming language.

def bubleSortPython(arraytoSort):
    swapFlag = True
    while swapFlag:
        swapFlag= False
        for i in range(len(arraytoSort)-1):
            if arraytoSort[i] > arraytoSort[i+1]:
                arraytoSort[i], arraytoSort[i+1] = arraytoSort[i+1], arraytoSort[i]

                print("Swapped: {} with {}".format(arraytoSort[i], arraytoSort[i+1]))
                swapFlag = True
    return arraytoSort
arraytoSort = [8,2,1,3,5,4]
print(bubleSortPython(arraytoSort))

Output

Swapped: 2 with 8
Swapped: 1 with 8
Swapped: 3 with 8
Swapped: 5 with 8
Swapped: 4 with 8
Swapped: 1 with 2
Swapped: 4 with 5

[1, 2, 3, 4, 5, 8]

Conclusion

The Bubble Sort code in Python is a very helpful way of sorting lists and placing adjacent values in their proper sequence. Do let us know if you have any further queries regarding bubble sorting in Python, we will be glad to assist you.


Advertisement
Advertisement
Advertisement