Skip to content

Commit

Permalink
Radix Sort in python
Browse files Browse the repository at this point in the history
  • Loading branch information
Vikum Sanjeewa committed Oct 16, 2024
1 parent 4143ab8 commit 73c0872
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added algorithms/.DS_Store
Binary file not shown.
51 changes: 51 additions & 0 deletions algorithms/ar-radsrt/python2/radix_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Function to get the maximum value in the list
def getMax(arr):
max_val = arr[0]
for i in arr:
if i > max_val:
max_val = i
return max_val

# Counting sort to sort elements based on the significant digit
def countingSort(arr, exp):
n = len(arr)
output = [0] * n # Output array to hold sorted elements
count = [0] * 10 # Initialize counting array (for base 10 digits)

# Store the count of occurrences
for i in range(n):
index = (arr[i] // exp) % 10
count[index] += 1

# Change count[i] so that count[i] contains the actual position
for i in range(1, 10):
count[i] += count[i - 1]

# Build the output array
i = n - 1
while i >= 0:
index = (arr[i] // exp) % 10
output[count[index] - 1] = arr[i]
count[index] -= 1
i -= 1

# Copy the output array to arr
for i in range(n):
arr[i] = output[i]

# Main function to implement Radix Sort
def radixSort(arr):
# Find the maximum number to determine the number of digits
max_val = getMax(arr)
# Do counting sort for every digit
exp = 1
while max_val // exp > 0:
countingSort(arr, exp)
exp *= 10

# Example usage
arr = [170, 45, 75, 90, 802, 24, 2, 66]
print("Original array:", arr)
radixSort(arr)
print("Sorted array:", arr)

0 comments on commit 73c0872

Please sign in to comment.