Advertisement
Python253

insertion_sort_demo

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