So basically I want to figure out how to reverse a counting sort. This is the code I found:
def countingSort(myList):
maxValue = 0
for i in range(len(myList)):
if myList[i] > maxValue:
maxValue = myList[i]
buckets = [0] * (maxValue + 1)
for i in myList:
buckets[i] += 1
i = 0
for j in range(maxValue + 1):
for a in range(buckets[j]):
myList[i] = j
i += 1
return myList
if __name__ == '__main__':
sortedList = countingSort([1,23,4,5,6,7,8])
print(sortedList)
So I decided to change line 16 to this:
i -= 1
The results are very close, but I get this:
[1, 23, 8, 7, 6, 5, 4]
My issue is that 1 is before 23 for some reason. I tried reverting some of the other signs, but it usually results in an out of bounds error. Any ways to work around it?
sortedfunction or.sortmethod?