Advertisement
Python253

merge_sort_demo

Jun 24th, 2024
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.84 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: merge_sort_demo.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    This script demonstrates the Merge Sort algorithm by sorting an array of integers.
  10.  
  11. Requirements:
  12.    - Python 3.x
  13.  
  14. Functions:
  15.    - merge_sort(arr):
  16.        Perform merge sort on the given list `arr`.
  17.  
  18. Usage:
  19.    - Run the script to see the Merge Sort algorithm in action.
  20.  
  21. Example:
  22.  
  23.                :: MERGE SORTING DEMO ::
  24.  
  25.        Original array: [12, 11, 13,  5,  6,  7]
  26.        Sorted array:   [ 5,  6,  7, 11, 12, 13]
  27.  
  28.        Merge Sorting Complete!
  29.  
  30.            Exiting Program...   GoodBye!
  31.  
  32. Additional Notes:
  33.    - Merge Sort is an efficient sorting algorithm with a time complexity of O(n log n).
  34. """
  35.  
  36. def merge_sort(arr):
  37.     """
  38.    Perform merge 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.     if len(arr) > 1:
  47.         mid = len(arr) // 2
  48.         L = arr[:mid]
  49.         R = arr[mid:]
  50.  
  51.         merge_sort(L)
  52.         merge_sort(R)
  53.  
  54.         i = j = k = 0
  55.         while i < len(L) and j < len(R):
  56.             if L[i] < R[j]:
  57.                 arr[k] = L[i]
  58.                 i += 1
  59.             else:
  60.                 arr[k] = R[j]
  61.                 j += 1
  62.             k += 1
  63.  
  64.         while i < len(L):
  65.             arr[k] = L[i]
  66.             i += 1
  67.             k += 1
  68.  
  69.         while j < len(R):
  70.             arr[k] = R[j]
  71.             j += 1
  72.             k += 1
  73.  
  74. def main():
  75.     """
  76.    Main function to demonstrate merge sort.
  77.  
  78.    It initializes an example array, sorts it using merge_sort function,
  79.    and prints the original and sorted arrays.
  80.    """
  81.     print("\n\t\t:: MERGE SORTING DEMO ::\n")
  82.    
  83.     # Example array to sort
  84.     arr = [12, 11, 13, 5, 6, 7]
  85.  
  86.     # Print the original array
  87.     original_arr_str = "["
  88.     for index, num in enumerate(arr):
  89.         if index == 0:
  90.             original_arr_str += f"{num:2}"  # Add one space for single-digit numbers
  91.         else:
  92.             original_arr_str += f", {num:2}"  # Add one space for single-digit numbers
  93.     original_arr_str += "]"
  94.     print("Original array:", original_arr_str)
  95.  
  96.     # Sort the array using merge sort
  97.     merge_sort(arr)
  98.  
  99.     # Print the sorted array
  100.     sorted_arr_str = "["
  101.     for index, num in enumerate(arr):
  102.         if index == 0:
  103.             sorted_arr_str += f"{num:2}"  # Add one space for single-digit numbers
  104.         else:
  105.             sorted_arr_str += f", {num:2}"  # Add one space for single-digit numbers
  106.     sorted_arr_str += "]"
  107.     print("Sorted array:  ", sorted_arr_str)
  108.    
  109.     # Print completion message
  110.     print("\nMerge Sorting Complete!\n\n\tExiting Program...   GoodBye!")
  111.  
  112. if __name__ == "__main__":
  113.     main()
  114.  
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement