简体   繁体   English

Python 如何解决基准计数排序的最大值要求

[英]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): def 计数排序(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'给出错误 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.不过,我建议改为计算maxval

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.我认为您将从使用collections.Counter stdlib 中的 collections.Counter 中受益。

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. collections.Counter不必针对大小进行初始化,并且对于您未更改的任何值,它都会报告 0。

Bonus tip: A lot of your code would benefit from Pythons list comprehensions ( beginner guide ).额外提示:您的许多代码都将从 Python 列表推导中受益( 初学者指南)。 Trust me, they're really worth the effort learning.相信我,他们真的值得努力学习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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