Bubble Sort in Descending 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 a Descending Order using the Bubble Sort algorithm.

Bubble Sort is a simple sorting algorithm that works by repeatedly stepping through the list, comparing adjacent elements, and swapping them if they are in the wrong order. For descending order, we'll swap elements if the current element is smaller than the next element. Let's dive into this algorithm using Python.

2. Program Overview

1. bubble_sort_descending(): The function implementing the bubble sort algorithm for sorting in descending order.

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

3. An example list to demonstrate the sorting process.

3. Code Program

def bubble_sort_descending(arr):
    n = len(arr)
    
    # Traverse through all elements in the list
    for i in range(n):
        # Flag to indicate if any swap occurred during this iteration
        swapped = False
        
        # The last i elements are already in their correct position, so we can skip them
        for j in range(0, n-i-1):
            # Traverse the list from 0 to n-i-1, swap if the current element is smaller than the next element
            if arr[j] < arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
                swapped = True
        
        # If no elements were swapped in the inner loop, it means the list is sorted
        if not swapped:
            break

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

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

Output:

Original list is:
64 34 25 12 22 11 90 

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

4. Step By Step Explanation

1. bubble_sort_descending(): This function sorts the list in descending order using the Bubble Sort algorithm. It uses nested loops: the outer loop guarantees that we iterate through the list enough times to sort it, while the inner loop compares and swaps elements as necessary.

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

3. Driver code: Here, we have provided an example list to showcase the sorting functionality.

The key difference in descending Bubble Sort is the comparison inside the inner loop. For descending order, we swap an element with the next one if it's smaller. If during one full pass no swaps are made, it indicates that the list is already sorted, and we can exit the loop early.

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