Signup/Sign In

Bubble Sort Program In Python

Posted in Technology   LAST UPDATED: DECEMBER 28, 2021

    For most individuals, Bubble Sort is likely the first sorting algorithm they learned about in their Computer Science education. It's incredibly intuitive and straightforward to "translate" into code, which is vital for rookie software engineers so they can ease themselves into translating ideas into a form that can be performed on a computer.

    What is Bubble Sort?

    Bubble Sort is a simple algorithm that is used to sort a given set of n elements provided in form of an array with n number of elements. Bubble Sort compares all the element one by one and sort them based on their values.

    If the given array has to be sorted in ascending order, then bubble sort will start by comparing the first element of the array with the second element, if the first element is greater than the second element, it will swap both the elements, and then move on to compare the second and the third element, and so on.

    If we have total n elements, then we need to repeat this process for n-1 times.

    It is known as bubble sort, because with every complete iteration the largest element in the given array, bubbles up towards the last place or the highest index, just like a water bubble rises up to the water surface.

    You can learn more about Bubble Sort Algorithm and its applications from here.

    Working of Bubble Sort

    Suppose we are attempting to sort the items in ascending order.

    1. First Iteration

    1. Starting from the first index, compare the first and the second item.
    2. If the first element is more significant than the second element, they are switched.
    3. Now, compare the second and the third components. Swap them if not in order.
    4. The procedure above carries on till the final piece.

    2. Remaining Iteration

    A similar procedure carries on for the remaining iterations.

    1. After each iteration, the biggest element among the unsorted elements is put at the end.
    2. In each cycle, the comparison takes place up to the final unsorted element.
    3. The array is sorted when all the unsorted items are put in their right spots.

    Bubble Sort Algorithm

    bubbleSort(array)
      for i <- 1 to indexOfLastUnsortedElement-1
        if leftElement > rightElement
          swap leftElement and rightElement
    end bubbleSort

    Bubble Sort Code in Python

    # 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, 8, 3]
    
    bubbleSort(data)
    
    print('Sorted Array in Ascending Order:')
    print(data)

    Output:


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

    Run Bubble Sort Program in Our Python Compiler

    Author:
    Adarsh Kumar Singh is a technology writer with a passion for coding and programming. With years of experience in the technical field, he has established a reputation as a knowledgeable and insightful writer on a range of technical topics.
    algorithmbubble-sortpython
    IF YOU LIKE IT, THEN SHARE IT

    RELATED POSTS