Python program for bubble sort

In this Python tutorial, we will discuss how to implement the bubble sort program in Python and also understand the concepts of the bubble sort with a Practical example. Along with that, we will cover the following given topics.

  • Python program for bubble sort
  • Python program for bubble sort using for loop
  • Python program for bubble sort with using function
  • Write a Python Program for bubble sort using the list
  • Python bubble sort list comprehension

Python program for bubble sort

  • By comparing two adjacent values, the Bubble Sort sorting algorithm arranges list items in ascending order. If the first value is greater than the second value, the second value takes the first value’s place and the first value moves down. There is no swapping if the first value is less than the second.
  • The first iteration involves the comparison and swapping operations in order to rank the values ascendingly. A comparison is made between the first and second index values. Swapping happens and is repeated till the end of the criteria are fulfilled.

Working of bubble sort:

  • Here is the given list of values, which stores the integer numbers
  • new_list = [25, 17, 7, 14, 6, 3]
  • Here the algorithm sort the elements –
  • First iteration
  • [25, 17, 7, 14, 6, 3]
    It will equate the first two values and here 25>17 then swap with each other. Now we get a new list –
  • [17, 25, 7, 14, 6, 3]
  • In the second comparison, 25 > 7 then swapping happen –
  • [17, 7, 25, 14, 6, 3]
  • In the third comparison, 25>14 then swap –
  • [17, 7, 14, 25, 6, 3]
  • In the fourth comparison, 25>6 then swap –
    [17, 7, 14, 6, 25, 3]
    In the fifth comparison, 25>3 then swap-
  • [17, 7, 14, 6, 3, 25]
  • Here the first iteration is complete and we get the largest element at the end. Now we need to the len(new_list1) –

Algorithm of bubble sort

  • Develop the Bubble Sort() function.
  • Provide a list of parameters to the function.
  • For access to each list, create a loop.
  • build a loop to evaluate list items
  • Compare two related objects
  • Swap components if they aren’t in the right order The outcome is a sorted list.
  • Print the list.

Let’s take an example and check and discuss the programming part of the bubble sort operation in Python.

Source Code:

def bubbleSort(new_list):
 
  for l in range(len(new_list)):
   
    for m in range(0, len(new_list) - l - 1):
      
      if new_list[m] > new_list[m + 1]:
          
        temp = new_list[m]
        new_list[m] = new_list[m+1]
        new_list[m+1] = temp

new_list =  [25, 17, 7, 14, 6, 3]
print("The input list is: ",new_list )

bubbleSort(new_list)
print('The Sorted list is: ', new_list)

Here is the implementation of the following given code

Python program for bubble sort
Python program for bubble sort

This is how we can create a program of bubble sort in Python.

Read Python Dictionary sort

Python program for bubble sort using for loop

  • It is a basic sorting algorithm that iteratively moves over the list to be sorted, comparing each pair of adjacent items, and swapping them if they are not in the right order. Before the list is sorted, the stages are repeated until no more swaps are required.
  • If the first and second elements in the list aren’t in the right order, compare them and swap them. If the second and third elements are in the wrong sequence, compare them and swap them. Continue in a similar way up to the final element of the list.
  • Repeat all of the following steps until the list is sorted.

Example:

new_list = []
new_val = int(input(" Enter the Total value : "))
for m in range(new_val):
    result = int(input("Enter the %d values : " %m))
    new_list.append(result)

for m in range(new_val -1):
    for k in range(new_val - m - 1):
        if(new_list [k] > new_list [k + 1]):
             temp = new_list [k]
             new_list [k] = new_list [k + 1]
             new_list [k + 1] = temp
 
print("Bubble sort : ", new_list )

Here is the Output of the following given code

Python program for bubble sort using for loop
Python program for bubble sort using for loop

In this example, we have understood how to create a bubble sort program in Python by using for loop method.

Read How to find perfect number in Python

Python program for bubble sort with using function

One pair of adjacent elements in an array is evaluated at a time using the Bubble Sort algorithm. If the first element in the pair is larger than the second, the locations of the two elements are swapped, otherwise, we just move on.

Example:

def bubbleSort(new_list):
 
  for l in range(len(new_list)):
   
    for m in range(0, len(new_list) - l - 1):
      
      if new_list[m] > new_list[m + 1]:
          
        temp = new_list[m]
        new_list[m] = new_list[m+1]
        new_list[m+1] = temp

new_list =  [25, 17, 7, 14, 6, 3]
print("The input list is: ",new_list )

bubbleSort(new_list)
print('The Sorted list is: ', new_list)

You can refer to the below Screenshot

Python program for bubble sort without using function
Python program for bubble sort with using function

This is how to create sorted a list in ascending order by using a function in Python.

Write a Python Program for bubble sort using a list

  • The simplest sorting algorithm is bubble sort. Elements can be sorted in either ascending or descending order using this method. Its name alludes to how the algorithm operates, in which the largest element in the list “bubbles up” from the list with each new iteration.
  • The first iteration involves the comparison and swapping operations in order to rank the values ascendingly. A comparison is made between the first and second index values. Swapping happens and is repeated till the end of the criteria are fulfilled.

Example:

new_list = []
new_val = int(input("Enter the total values : "))
for n in range(new_val):
    value = int(input("Enter the %d value : " %n))
    new_list.append(value)

n = 0
while(n < new_val -1):
    k = 0
    while(k < new_val - n - 1):
        if(new_list[k] > new_list[k + 1]):
             temp = new_list[k]
             new_list[k] = new_list[k + 1]
             new_list[k + 1] = temp
        k = k + 1
    n = n + 1

print("Given list asscending order : ", new_list)

Here is the implementation of the following given code

Write a Python Program for bubble sort using the list
Write a Python Program for bubble sort using the list

This is how we can create a Python program for bubble sort by using a list.

Read How to reverse a number in Python

Python bubble sort list comprehension

  • Basically, list comprehension involves creating new lists from iterables that already exist. It can also be thought of as a more attractive appearance and simple representation of for and if loops. Comparatively, list comprehensions are faster than for loops.
  • In this example, we will use the input() function and take the input from the user and use the list comprehension method to iterate the values

Example:

new_input_val = input("Enter the element: ")
 
result = [int(m) for m in new_input_val.split()]
 
count = len(result)
 
for outer in range(count-1): 
    for n in range(count-outer-1):
        if result[n] > result[n+1]:
            
            result[n],result[n+1] = result[n+1],result[n]
 
print(result)

Here is the Screenshot of the following given code

Python bubble sort list comprehension
Python bubble sort list comprehension

In this example, we have understood how to create a bubble sort by using list comprehension in Python.

You may like the following Python tutorials:

In this tutorial, we have discussed how to execute a bubble sort program in Python and also understand the concepts of the bubble sort with a Practical example. Along with that, we have covered the following given topics.

  • Python program for bubble sort
  • Python program for bubble sort using for loop
  • Python program for bubble sort with using function
  • Write a Python Program for bubble sort using the list
  • Python bubble sort list comprehension