Advertisement
Python253

bubble_sort_demo

Jun 23rd, 2024
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.56 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: bubble_sort_demo.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    This script demonstrates the bubble sort algorithm by sorting an array of integers.
  10.  
  11. Requirements:
  12.    - Python 3.x
  13.  
  14. Functions:
  15.    - bubble_sort(arr):
  16.        Perform bubble sort on the given list `arr`.
  17.        
  18. Usage:
  19.    - Run the script to see the bubble sort algorithm in action.
  20.  
  21. Additional Notes:
  22.    - Bubble sort is not efficient for large datasets compared to more advanced sorting algorithms.
  23. """
  24.  
  25. def bubble_sort(arr):
  26.     """
  27.    Perform bubble sort on the given list `arr` in place.
  28.  
  29.    Args:
  30.        arr (list): List of integers to be sorted in place.
  31.  
  32.    Returns:
  33.        None. The list `arr` is sorted in place.
  34.  
  35.    Example Output:
  36.    
  37.                :: BUBBLE SORTING DEMO ::
  38.  
  39.        Original array: [69, 42, 25, 12, 22, 17, 79, 90,  7, 99]
  40.        Sorted array:   [ 7, 12, 17, 22, 25, 42, 69, 79, 90, 99]
  41.  
  42.        Bubble Sorting Complete!
  43.  
  44.            Exiting Program...   GoodBye!        
  45.    """
  46.     n = len(arr)
  47.     for i in range(n):
  48.         for j in range(0, n-i-1):
  49.             if arr[j] > arr[j+1]:
  50.                 arr[j], arr[j+1] = arr[j+1], arr[j]
  51.  
  52. def main():
  53.     """
  54.    Main function to demonstrate bubble sort.
  55.  
  56.    It initializes an example array, sorts it using bubble_sort function,
  57.    and prints the original and sorted arrays.
  58.    """
  59.     print("\n\t\t:: BUBBLE SORTING DEMO ::\n")
  60.    
  61.     # Example array to sort
  62.     arr = [69, 42, 25, 12, 22, 17, 79, 90, 7, 99]
  63.  
  64.     # Print the original array
  65.     original_arr_str = "["
  66.     for index, num in enumerate(arr):
  67.         if index == 0:
  68.             original_arr_str += f"{num:2}"  # Add a leading space for single-digit numbers
  69.         else:
  70.             original_arr_str += f", {num:2}"  # Add a leading space for single-digit numbers
  71.     original_arr_str += "]"
  72.     print("Original array:", original_arr_str)
  73.  
  74.     # Sort the array using bubble sort
  75.     bubble_sort(arr)
  76.  
  77.     # Print the sorted array
  78.     sorted_arr_str = "["
  79.     for index, num in enumerate(arr):
  80.         if index == 0:
  81.             sorted_arr_str += f"{num:2}"  # Add a leading space for single-digit numbers
  82.         else:
  83.             sorted_arr_str += f", {num:2}"  # Add a leading space for single-digit numbers
  84.     sorted_arr_str += "]"
  85.     print("Sorted array:  ", sorted_arr_str)
  86.    
  87.     # Print completion message
  88.     print("\nBubble Sorting Complete!\n\n\tExiting Program...   GoodBye!")
  89.  
  90. if __name__ == "__main__":
  91.     main()
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement