Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # program to implement counting sort algorithm in python
- def countingSort(myList):
- # finding the length, min, max of the recieved list of numbers
- length = len(myList)
- minElement = min(myList)
- maxElement = max(myList)
- # creating a count list with initial values as zeros
- countList = [0 for i in range(minElement, maxElement+1)]
- # counting number of times each element occurs in the list and storing the count in the coutList
- for element in myList:
- countList[element - minElement] = countList[element - minElement] + 1
- # starting from the min element upto max element
- # we insert values according to their count in the new list in sorted order
- element = minElement
- myList.clear()
- for Count in countList:
- myList += [element for i in range(Count)]
- element += 1
- return myList
- print("Program to implement Counting sort algorithm in python")
- myList = list(map(int, input("\nEnter the elements to sorted as spaced integers : ").strip().split()))
- print("The list before sorting is : ")
- print(myList)
- myList = countingSort(myList)
- print("\nThe sorted list is : ")
- print(myList)
Add Comment
Please, Sign In to add comment