Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: radix_sort_demo.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script demonstrates the Radix Sort algorithm by sorting an array of integers.
- Requirements:
- - Python 3.x
- Functions:
- - counting_sort(arr, exp):
- Perform counting sort on the array `arr` based on the exponent `exp`.
- - radix_sort(arr):
- Perform radix sort on the given list `arr` using counting_sort as a subroutine.
- Usage:
- - Run the script to see the Radix Sort algorithm in action.
- Example Output:
- :: RADIX SORTING DEMO ::
- Original array: [170, 45, 75, 90, 802, 24, 2, 66]
- Sorted array: [ 2, 24, 45, 66, 75, 90, 170, 802]
- Radix Sorting Complete!
- Exiting Program... GoodBye!
- Additional Notes:
- - Radix Sort is efficient for sorting integers, especially when the range of numbers is known and limited.
- """
- def counting_sort(arr, exp):
- """
- Perform counting sort on the array `arr` based on the exponent `exp`.
- Args:
- arr (list): List of integers to be sorted.
- exp (int): Exponent used to sort elements.
- Returns:
- None. The list `arr` is sorted in place based on the exponent `exp`.
- """
- n = len(arr)
- output = [0] * n
- count = [0] * 10
- for i in range(n):
- index = arr[i] // exp
- count[index % 10] += 1
- for i in range(1, 10):
- count[i] += count[i - 1]
- i = n - 1
- while i >= 0:
- index = arr[i] // exp
- output[count[index % 10] - 1] = arr[i]
- count[index % 10] -= 1
- i -= 1
- for i in range(n):
- arr[i] = output[i]
- def radix_sort(arr):
- """
- Perform radix sort on the given list `arr` using counting_sort as a subroutine.
- Args:
- arr (list): List of integers to be sorted in place.
- Returns:
- None. The list `arr` is sorted in place using radix sort.
- """
- max1 = max(arr)
- exp = 1
- while max1 // exp > 0:
- counting_sort(arr, exp)
- exp *= 10
- def main():
- """
- Main function to demonstrate radix sort.
- It initializes an example array, sorts it using radix_sort function,
- and prints the original and sorted arrays.
- """
- print("\n\t\t:: RADIX SORTING DEMO ::\n")
- # Example array to sort
- arr = [170, 45, 75, 90, 802, 24, 2, 66]
- # Print the original array
- original_arr_str = "["
- for index, num in enumerate(arr):
- if index == 0:
- original_arr_str += f"{num:3}" # Add two spaces for single-digit numbers
- else:
- original_arr_str += f", {num:3}" if num >= 10 else f", {num:2}" # Add one space for two-digit numbers
- original_arr_str += "]"
- print("Original array:", original_arr_str)
- # Sort the array using radix sort
- radix_sort(arr)
- # Print the sorted array
- sorted_arr_str = "["
- for index, num in enumerate(arr):
- if index == 0:
- sorted_arr_str += f"{num:3}" # Add two spaces for single-digit numbers
- else:
- sorted_arr_str += f", {num:3}" if num >= 10 else f", {num:2}" # Add one space for two-digit numbers
- sorted_arr_str += "]"
- print("Sorted array: ", sorted_arr_str)
- # Print completion message
- print("\nRadix Sorting Complete!\n\n\tExiting Program... GoodBye!")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement