Advertisement
Python253

selection_sort_demo

Jun 24th, 2024
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.66 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: selection_sort_demo.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    This script demonstrates the selection sort algorithm by sorting an array of integers.
  10.  
  11. Requirements:
  12.    - Python 3.x
  13.  
  14. Functions:
  15.    - selection_sort(arr):
  16.        Perform selection sort on the given list `arr`.
  17.        
  18. Usage:
  19.    - Run the script to see the selection sort algorithm in action.
  20.  
  21. Additional Notes:
  22.    - Selection sort is not efficient for large datasets compared to more advanced sorting algorithms.
  23. """
  24.  
  25. def selection_sort(arr):
  26.     """
  27.    Perform selection 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.                :: SELECTION 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.        Selection Sorting Complete!
  43.  
  44.            Exiting Program...   GoodBye!        
  45.    """
  46.     n = len(arr)
  47.     for i in range(n):
  48.         min_idx = i
  49.         for j in range(i+1, n):
  50.             if arr[j] < arr[min_idx]:
  51.                 min_idx = j
  52.         arr[i], arr[min_idx] = arr[min_idx], arr[i]
  53.  
  54. def main():
  55.     """
  56.    Main function to demonstrate selection sort.
  57.  
  58.    It initializes an example array, sorts it using selection_sort function,
  59.    and prints the original and sorted arrays.
  60.    """
  61.     print("\n\t\t:: SELECTION SORTING DEMO ::\n")
  62.    
  63.     # Example array to sort
  64.     arr = [69, 42, 25, 12, 22, 17, 79, 90, 7, 99]
  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 a leading space for single-digit numbers
  71.         else:
  72.             original_arr_str += f", {num:2}"  # Add a leading space for single-digit numbers
  73.     original_arr_str += "]"
  74.     print("Original array:", original_arr_str)
  75.  
  76.     # Sort the array using selection sort
  77.     selection_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 a leading space for single-digit numbers
  84.         else:
  85.             sorted_arr_str += f", {num:2}"  # Add a leading space for single-digit numbers
  86.     sorted_arr_str += "]"
  87.     print("Sorted array:  ", sorted_arr_str)
  88.    
  89.     # Print completion message
  90.     print("\nSelection 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