Skip to content
Snippets Groups Projects
Commit 2f8d756a authored by nilsl99's avatar nilsl99
Browse files

Implemented Countingsort

parent 1caa1a69
No related branches found
No related tags found
No related merge requests found
...@@ -74,7 +74,7 @@ def InsertionSort(array): ...@@ -74,7 +74,7 @@ def InsertionSort(array):
return sorted_array return sorted_array
def CountingSort(array): def CountingSort(array, k=None):
'''Sort the array by counting the ocjence of each element in the array. '''Sort the array by counting the ocjence of each element in the array.
Parameters Parameters
...@@ -87,4 +87,22 @@ def CountingSort(array): ...@@ -87,4 +87,22 @@ def CountingSort(array):
sorted_array: a sorted copy of the array sorted_array: a sorted copy of the array
''' '''
return if k is None:
k = max(array)
key_array = [0]*(k+1)
# Zählen der Elemente
for i in array:
key_array[i] += 1
sorted_array = [0]*len(array)
i = 0
for j, k in enumerate(key_array):
while k > 0:
sorted_array[i] = j
i += 1
k -= 1
return sorted_array
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment