Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Algorithms covered so far:
| Insertion Sort | insertion_sort |
| Heap Sort | heap_sort |
| Merge Sort | merge_sort |

| Radix Sort | radix_sort |
# Usage:

Install
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def get_random_array(length):
array_size = 50

algorithms = [s.bubble_sort, s.heap_sort, s.selection_sort,
s.insertion_sort, s.quick_sort, s.insertion_sort, s.merge_sort]
s.insertion_sort, s.quick_sort, s.insertion_sort, s.merge_sort,s.radix_sort]

if len(sys.argv) < 2:
print('Usage:', 'python', sys.argv[0], 'function_name', '\n')
Expand Down
58 changes: 58 additions & 0 deletions sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def insertion_sort(nums): # n^2
# Insert the item
nums.set(j + 1, item_to_insert)






def heap_sort(nums): # n * logn

Expand Down Expand Up @@ -212,3 +216,57 @@ def _quick_sort(items, low, high):
_quick_sort(items, split_index + 1, high)

_quick_sort(nums, 0, nums.get_len() - 1)


# Python program for implementation of Radix Sort BY Saugata Debnath

# A function to do counting sort of arr[] according to
# the digit represented by exp.
def counting_sort(arr, exp1):

n = len(arr)

# The output array elements that will have sorted arr
output = [0] * (n)

# initialize count array as 0
count = [0] * (10)

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

# Change count[i] so that count[i] now contains actual
# position of this digit in output array
for i in range(1,10):
count[i] += count[i-1]

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

# Copying the output array to arr[],
# so that arr now contains sorted numbers
i = 0
for i in range(0,len(arr)):
arr[i] = output[i]

# Method to do Radix Sort
def radix_sort(arr):

# Find the maximum number to know number of digits
max1 = max(arr)

# Do counting sort for every digit. Note that instead
# of passing digit number, exp is passed. exp is 10^i
# where i is current digit number
exp = 1
while max1/exp > 0:
counting_sort(arr,exp)
exp *= 10

2 changes: 1 addition & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ def test(sorting_function):


functions = [s.bubble_sort, s.heap_sort, s.selection_sort,
s.insertion_sort, s.quick_sort, s.merge_sort]
s.insertion_sort, s.quick_sort, s.merge_sort,s.radix_sort]
for function in functions:
print('Testing: ', function.__name__.ljust(16), test(function))