Advertisement
Python253

quick_sort_demo

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