diff --git a/Python/counting_sort.py b/Python/counting_sort.py new file mode 100644 index 0000000..2990ece --- /dev/null +++ b/Python/counting_sort.py @@ -0,0 +1,35 @@ +# Counting sort in Python programming + + +def countingSort(array): + size = len(array) + output = [0] * size + + # Initialize count array + count = [0] * 10 + + # Store the count of each elements in count array + for i in range(0, size): + count[array[i]] += 1 + + # Store the cummulative count + for i in range(1, 10): + count[i] += count[i - 1] + + # Find the index of each element of the original array in count array + # place the elements in output array + i = size - 1 + while i >= 0: + output[count[array[i]] - 1] = array[i] + count[array[i]] -= 1 + i -= 1 + + # Copy the sorted elements into original array + for i in range(0, size): + array[i] = output[i] + + +data = [4, 2, 2, 8, 3, 3, 1] +countingSort(data) +print("Sorted Array in Ascending Order: ") +print(data) \ No newline at end of file