Signup/Sign In

Bubble Sort Program in Python Language

Learn Pythono Tutorial

In this tutorial, we will learn about the bubble sort algorithm and its implementation in Python. It is the simplest sorting algorithm that is used when the array is not too large because of its high time complexity. Bubble sort is a sorting algorithm that compares two adjacent elements and swaps them until they are not in the required sorted order. It is to be noted that in general if nothing is mentioned we will consider the required order to be ascending order.

As the output, we are required to get the sorted array.

Look at the given example to understand the working with input and output.

Input:

array: {3 5 1 2 6}

Output:

array: {1 2 3 5 6}

Input:

array: {56 9 11 7 60}

Output:

array: {7 9 11 56 60}

Bubble Sort Algorithm

1- Compare and swap iteration

Step1: Starting from the first index, compare the first and the second elements.

Step2: If the first element is greater than the second element, they are swapped.

Step3: Now, compare the second and the third elements. Swap them if they are not in order.

Step4: The above process goes on until the last element.

2- Process the remaining iteration

Step5: The same process goes on for the remaining iterations. After each iteration, the largest element among the unsorted elements is placed at the end.

Step 6: The array is sorted when all the unsorted elements are placed at their correct positions.

Bubble Sort Python program 1

# Bubble sort in Python

def bubbleSort(array):
    
  # loop to access each array element
  for i in range(len(array)):

    # loop to compare array elements
    for j in range(0, len(array) - i - 1):

      # compare two adjacent elements
      # change > to < to sort in descending order
      if array[j] > array[j + 1]:

        # swapping elements if elements
        # are not in the intended order
        temp = array[j]
        array[j] = array[j+1]
        array[j+1] = temp


data = [-2, 45, 0, 11, -9]

bubbleSort(data)

print('Sorted Array in Ascending Order:')
print(data)

Sorted Array in Ascending Order:
[-9, -2, 0, 11, 45]

Bubble Sort Python program 2 (optimized bubble sort solution)

# Optimized Bubble sort in Python

def bubbleSort(array):
    
  # loop through each element of array
  for i in range(len(array)):
        
    # keep track of swapping
    swapped = False
    
    # loop to compare array elements
    for j in range(0, len(array) - i - 1):

      # compare two adjacent elements
      # change > to < to sort in descending order
      if array[j] > array[j + 1]:

        # swapping occurs if elements
        # are not in the intended order
        temp = array[j]
        array[j] = array[j+1]
        array[j+1] = temp

        swapped = True
          
    # no swapping means the array is already sorted
    # so no need for further comparison
    if not swapped:
      break

data = [-2, 45, 0, 11, -9]

bubbleSort(data)

print('Sorted Array in Ascending Order:')
print(data)

Sorted Array in Ascending Order:
[-9, -2, 0, 11, 45]

Conclusion

Here, we have seen one of the most popular sorting techniques i.e. bubble sort using two different methods where the latter one provides the more optimized solution to the problem in terms of the complexity of the program.