Bubble Sort in Python

In this section, we will talk about bubble sorting in Python. If you want to learn about bubble sorting in Python, you are at an accurate platform. Here, we will provide you with all the steps on how you can perform the bubble sorting in Python. Bubble sorting is also an algorithm of sorting. The list is shuffled again and again before the exchange is required; this process shows that the array is sorted properly. This article is related to bubble sorting. Here, we will discuss this type of sorting with the help of some programming examples.

Bubble Sort in Python

In this section, we will provide some details about bubble sorting. Bubble sort iteratively scans the list, compares the items, and replaces any that are out of order. The list is sent through again and again until it is sorted. This procedure is repeated until all the values in an array or list are compared. If swapping is needed in any case, the elements are swapped for sorting. In bubble sorting, we perform the checking in iterations with the help of different loops like the “for” and “while” loops.

Every iteration is typically referred to as a “pass” in sorting. This is all about the bubble sorting details in Python. As the sorting process continues, smaller components (if we are sorting in ascending order) “bubble” at the top of the list and gives the method a comparison sort and its name.

Let’s say we want to arrange a list of items in an orderly fashion which is in random order. Consider the following list:


To swap two adjacent components if the first value is greater than the second, we iterate through the list. The outcome is as follows:


Hopefully, you now have some understanding of bubble sorting. Now, let’s discuss some programming examples for better understanding.

Example 1:
First, we discuss the process of bubble sorting with the help of a simple example. With this example program, you can easily perform the bubble sorting in your Python applications. The reference code of this example is as follows. Refer to this code and try to understand it line by line:

def bubble_sort(arr1):
    num = len(arr1)
    for i in range(num):
        for m in range(0, num - i - 1):
            if arr1[m] > arr1[m + 1]:
                arr1[m], arr1[m + 1] = arr1[m + 1], arr1[m]
    return arr1

We define a function that we call the process of bubble sorting and the name of the function which is “bubble_sort”. In this function, we pass the array that we define for sorting. Here, the array is named “arr1”. We take another variable named “num” and assign the length of the array to this variable. Now, we use a “for loop” which is used to search the index of the list that contains the different types of elements. We take a variable named “i”. This loop checks all the elements of the array. We take a loop that is used to access each element of the array to search the elements that one is greater and to compare the adjacent elements. The outer “for loop” with the “i” variable runs for “num” times, where “num” is the length of the list. The inner “for loop” with the “m” variable runs from 0 to num-i-1.

In each iteration of the inner for loop, we compare the current element (arr1[m]) with the next element (arr1[m+1]). In the case that the current element is larger than the next element, we exchange them. Accordingly, the largest element bubbles up to the very last position on the list after each iteration, followed by the second-largest element during the subsequent run, and so on. This process is repeated until the list is sorted.

The array is sorted after the outer “for loop” is finished. The bubble sort is one of the simplest sorting algorithms, but it is not the most efficient one because of its O(n^2) time complexity. This means that the time that is taken to sort the array increases exponentially as the number of elements in the array increases.

The output of this example is as follows:

[1, 10, 12, 25, 35, 44, 64]

Example 2:
We take another example for your help. The reference code of this example is attached in the following:

def bubbleSortArray(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n-i-1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
    return arr
list_element=[64, 44, 35, 25, 12, 1, 10]

Here, in the previous code, we see that we first define the function for bubble sort named “bubbleSortArray” in which we pass an “arr” array as a function argument. After that, we declare a variable named “n” which stores the length of an array.

Now, we use a “for” loop in which we initialize the “i” variable that limits the equal to the length of the array. In this “for” loop, we use another loop that takes the first element of an array. The range of this loop is less than the value of the index. Here, we compare the adjacent array values. If the value of the array is greater than the next value of an array, we swap the values and return these values of the array to the function parameter.

So, in this way, we can perform the sorting. Lastly, we initialize the array on which we apply the bubble storing. We pass the function in a print statement for the displayed result on the screen. The array is arranged in descending order in this implementation. The outer loop iterates through the array and the inner loop compares and swaps the adjacent elements if they are not stored.

To display the output, we pass the function in the print statement. The “list_element” is initialized with an array on which we perform the bubble sort.

The output of this array is attached in the following:

[1, 10, 12, 25, 35, 44, 64]


We can say that bubble sort uses a straightforward logic that works by repeating the adjacent elements if they are not in the correct order and that the bubble sort algorithm does not perform well when the array or list is in reverse order and all the elements of arrays are not sorted. This type of sorting is most powerful for academic purposes like searching and performing different tasks. Hopefully, you now understand this type of sorting in Python. You can easily implement these example codes in your Python application for more clarity.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content