简体   繁体   中英

Python how to address the max value requirement for benchmarking countsort

I have been trying to benchmark some different sorts and have gotten most of the problems out but count sort is proving awkward. Where do I give it a max value?

I have tried a few different methods but this

def counting_sort(arr, maxval):

n = len(arr)
m = maxval + 1
count = [0] * m               
for a in arr:
    count[a] += 1             
i = 0
for a in range(m):           
    for c in range(count[a]): # - emit 'count[a]' copies of 'a'
        arr[i] = a
        i += 1
return arr

Gave the error TypeError: counting_sort() missing 1 required positional argument: 'maxval'

So I thought I would try a method that didnt call a max value at the start

    arr = []
    for i in range(0, n, 1):
        arr.append(randint(0, 100))
    return arr

def counting_sort(arr):
    size = len(arr)
    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[arr[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[arr[i]] - 1] = arr[i]
        count[arr[i]] -= 1
        i -= 1

    # Copy the sorted elements into original array
    for i in range(0, size):
        arr[i] = output[i]

    return arr

num_runs = 10
elements = [100, 250, 500, 750, 1000, 1250, 2500, 3750, 5000]
def countrunTime():    

    stimes = []    
    for i in elements:
        arr = random_array(i)
        countresults = []
        for r in range(num_runs):

            start_time = time.time()

            counting_sort(arr)

            end_time = time.time()

            time_elapsed = end_time - start_time
            countresults.append(time_elapsed)
        s = round(mean(countresults),3)
        stimes.append(s)
    return stimes

Returns the error in counting_sort
    count[arr[i]] += 1
IndexError: list index out of range

If you don't give a default value to an argument you can't call the method without supplying it.

def counting_sort(arr, maxval):
    pass

counting_sort([])  # Will fail
counting_sort([], 123)  # Will work

I would suggest calculating maxval instead, though.

def counting_sort(arr):
    maxval = max(array)
    # ...

For the second error, you're trying to access an index greater than the length of the count array. I think you would benefit from using collections.Counter from pythons stdlib instead.

from collections import Counter
counter = Counter()
for value in arr:
   counter[value] += 1
print(counter)

collections.Counter does not have to be initialized for a size, and it reports 0 for any value you did not alter.

Bonus tip: A lot of your code would benefit from Pythons list comprehensions ( beginner guide ). Trust me, they're really worth the effort learning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM