|
| 1 | +""" |
| 2 | +Radix Sort Algorithm |
| 3 | +Time Complexity: O(d * (n + k)) where d is number of digits, k is range of digits |
| 4 | +Space Complexity: O(n + k) |
| 5 | +Author: Karanjot Singh |
| 6 | +Date: October 2025 |
| 7 | +Hacktoberfest 2025 |
| 8 | +""" |
| 9 | + |
| 10 | +def counting_sort_for_radix(arr, exp): |
| 11 | + """ |
| 12 | + Counting sort based on digit position (exp) |
| 13 | + Used as subroutine in radix sort |
| 14 | +
|
| 15 | + Args: |
| 16 | + arr: Array to sort |
| 17 | + exp: Current digit position (1, 10, 100, ...) |
| 18 | + """ |
| 19 | + n = len(arr) |
| 20 | + output = [0] * n |
| 21 | + count = [0] * 10 |
| 22 | + |
| 23 | + # Store count of occurrences |
| 24 | + for i in range(n): |
| 25 | + index = arr[i] // exp |
| 26 | + count[index % 10] += 1 |
| 27 | + |
| 28 | + # Change count[i] to contain actual position in output |
| 29 | + for i in range(1, 10): |
| 30 | + count[i] += count[i - 1] |
| 31 | + |
| 32 | + # Build output array |
| 33 | + i = n - 1 |
| 34 | + while i >= 0: |
| 35 | + index = arr[i] // exp |
| 36 | + output[count[index % 10] - 1] = arr[i] |
| 37 | + count[index % 10] -= 1 |
| 38 | + i -= 1 |
| 39 | + |
| 40 | + # Copy output array to arr |
| 41 | + for i in range(n): |
| 42 | + arr[i] = output[i] |
| 43 | + |
| 44 | + |
| 45 | +def radix_sort(arr): |
| 46 | + """ |
| 47 | + Radix sort implementation for non-negative integers |
| 48 | +
|
| 49 | + Sorts by processing digits from least significant to most significant |
| 50 | + Uses counting sort as stable sort for each digit |
| 51 | +
|
| 52 | + Args: |
| 53 | + arr: List of non-negative integers to sort (modified in place) |
| 54 | + """ |
| 55 | + if not arr: |
| 56 | + return |
| 57 | + |
| 58 | + # Find maximum number to determine number of digits |
| 59 | + max_num = max(arr) |
| 60 | + |
| 61 | + # Apply counting sort to sort elements based on place value |
| 62 | + exp = 1 |
| 63 | + while max_num // exp > 0: |
| 64 | + counting_sort_for_radix(arr, exp) |
| 65 | + exp *= 10 |
| 66 | + |
| 67 | + |
| 68 | +def radix_sort_copy(arr): |
| 69 | + """ |
| 70 | + Radix sort that returns sorted copy |
| 71 | +
|
| 72 | + Args: |
| 73 | + arr: List of non-negative integers to sort |
| 74 | +
|
| 75 | + Returns: |
| 76 | + Sorted copy of the list |
| 77 | + """ |
| 78 | + arr_copy = arr.copy() |
| 79 | + radix_sort(arr_copy) |
| 80 | + return arr_copy |
| 81 | + |
| 82 | + |
| 83 | +# Test cases |
| 84 | +if __name__ == "__main__": |
| 85 | + print("=== Radix Sort Algorithm ===\n") |
| 86 | + |
| 87 | + # Test case 1: Random array |
| 88 | + test1 = [170, 45, 75, 90, 802, 24, 2, 66] |
| 89 | + print(f"Original array: {test1}") |
| 90 | + sorted1 = radix_sort_copy(test1) |
| 91 | + print(f"Sorted array: {sorted1}") |
| 92 | + |
| 93 | + # Test case 2: Already sorted |
| 94 | + test2 = [1, 2, 3, 4, 5] |
| 95 | + print(f"\nOriginal array: {test2}") |
| 96 | + sorted2 = radix_sort_copy(test2) |
| 97 | + print(f"Sorted array: {sorted2}") |
| 98 | + |
| 99 | + # Test case 3: Reverse sorted |
| 100 | + test3 = [543, 321, 123, 54, 21] |
| 101 | + print(f"\nOriginal array: {test3}") |
| 102 | + sorted3 = radix_sort_copy(test3) |
| 103 | + print(f"Sorted array: {sorted3}") |
| 104 | + |
| 105 | + # Test case 4: Array with duplicates |
| 106 | + test4 = [100, 50, 200, 50, 100, 25] |
| 107 | + print(f"\nOriginal array: {test4}") |
| 108 | + sorted4 = radix_sort_copy(test4) |
| 109 | + print(f"Sorted array: {sorted4}") |
| 110 | + |
| 111 | + # Test case 5: Single digit numbers |
| 112 | + test5 = [9, 3, 7, 1, 5, 2, 8] |
| 113 | + print(f"\nOriginal array: {test5}") |
| 114 | + sorted5 = radix_sort_copy(test5) |
| 115 | + print(f"Sorted array: {sorted5}") |
| 116 | + |
| 117 | + # Test case 6: Large numbers |
| 118 | + test6 = [12345, 987, 54321, 123, 9876, 456] |
| 119 | + print(f"\nOriginal array: {test6}") |
| 120 | + sorted6 = radix_sort_copy(test6) |
| 121 | + print(f"Sorted array: {sorted6}") |
| 122 | + |
| 123 | + # Test case 7: Single element |
| 124 | + test7 = [42] |
| 125 | + print(f"\nOriginal array: {test7}") |
| 126 | + sorted7 = radix_sort_copy(test7) |
| 127 | + print(f"Sorted array: {sorted7}") |
| 128 | + |
| 129 | + print("\n✅ All test cases completed!") |
0 commit comments