Bubble Sort in Ascending Order in Python

1. Introduction

In this blog post, we will learn how to write a Python program to sort an array of integers in an Ascending Order using the Bubble Sort algorithm.

Bubble Sort is a straightforward comparison-based sorting algorithm. It repeatedly iterates through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. Let's explore this algorithm in Python.

2. Program Overview

1. bubble_sort(): The main function to execute the bubble sort algorithm.

2. print_list(): A utility function to display the list.

3. An example list to demonstrate the algorithm.

3. Code Program

def bubble_sort(arr):
    n = len(arr)
    
    # Traverse through all elements in the list
    for i in range(n):
        # Flag to indicate if any swap happened in this iteration
        swapped = False
        
        # Last i elements are already in place, no need to check them
        for j in range(0, n-i-1):
            # Traverse the list from 0 to n-i-1, swap if the element found is greater than the next element
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
                swapped = True
        
        # If no two elements were swapped by the inner loop, the list is sorted
        if not swapped:
            break

def print_list(arr):
    for i in arr:
        print(i, end=" ")
    print()

# Driver code to test above functions
if __name__ == "__main__":
    arr = [64, 34, 25, 12, 22, 11, 90]
    print("Original list is:")
    print_list(arr)
    
    bubble_sort(arr)
    
    print("\nSorted list in ascending order is:")
    print_list(arr)

Output:

Original list is:
64 34 25 12 22 11 90 

Sorted list in ascending order is:
11 12 22 25 34 64 90 

4. Step By Step Explanation

1. bubble_sort(): This function sorts the array using Bubble Sort. The process involves nested loops. The outer loop ensures we pass through the list enough times to ensure it's sorted. The inner loop performs the actual comparisons and swaps.

2. print_list(): A utility function to print the elements of the list.

3. Driver code: We demonstrate the functionality of bubble sort using a test list.

Bubble Sort is simple but not the most efficient sorting algorithm, especially for large lists. The main idea behind it is to repeatedly swap adjacent elements if they are in the wrong order. If during a pass no swaps are made, it means the list is sorted and we can break out of the loop.

Related Python Programs on Sorting Algorithms

  1. Bubble Sort in Ascending Order in Python
  2. Bubble Sort in Descending Order in Python
  3. Selection Sort in Ascending Order in Python
  4. Selection Sort in Descending Order in Python
  5. Insertion Sort in Descending Order in Python
  6. Insertion Sort in Ascending Order in Python
  7. Merge Sort in Ascending Order in Python
  8. Merge Sort in Descending Order in Python
  9. Quick Sort in Ascending Order in Python
  10. Quick Sort in Descending Order in Python
  11. Heap Sort in Ascending Order in Python
  12. Heap Sort in Descending Order in Python


Comments