Skip to content

Commit

Permalink
Merge pull request #63 from Raj-sharma01/countSortInPython
Browse files Browse the repository at this point in the history
added counting sort in python
  • Loading branch information
lilmistake authored Oct 26, 2023
2 parents 0cd4d82 + 9d905a5 commit 2af255d
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions DSA_Codesheet/Python/countingSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import List

def sortArray(N: List[int]) -> List[int]:
min_val, max_val = min(N), max(N)
count = [0] * (max_val - min_val + 1)

for num in N:
count[num - min_val] += 1

sorted_array = []
for i in range(len(count)):
sorted_array.extend([i + min_val] * count[i])

return sorted_array

if __name__ == "__main__":
# Input
n = int(input("Enter the number of elements: "))
user_input = list(map(int, input("Enter the elements separated by spaces: ").split()))

# Output the array before sorting
print("Array Before Sorting :-")
print(" ".join(map(str, user_input)))

# Sort the array
sorted_list = sortArray(user_input)

# Output the array after sorting
print("Array After Sorting :-")
print(" ".join(map(str, sorted_list)))

0 comments on commit 2af255d

Please sign in to comment.