简体   繁体   English

该存储桶排序实现在做什么?

[英]What is this bucket sort implementation doing?

This is my code for bucket sort in Python. 这是我在Python中进行存储桶排序的代码。

from random import randrange


def insertion_sort(aList):
    for i in range(1, len(aList)):
        for j in range(i, 0, -1):
            if aList[j] < aList[j-1]:
                aList[j], aList[j-1] = aList[j-1], aList[j]
    return aList

def bucket_sort(aList):
    buckets =  [[]] * len(aList)
    for index, value in enumerate(aList):
        buckets_index = value * len(aList) // (max(aList) + 1)
        buckets[buckets_index].append(value)

answer = []

for bucket in buckets:
    answer.extend(insertion_sort(bucket))
    # answer += insertion_sort(bucket)

print(buckets[0])
print("\n")
# return answer


aList = [randrange(10) for _ in range(100)]
print(aList)
print("\n")
answer = bucket_sort(aList)
#print(answer)

What is happening? 怎么了? When I run the code, I always find that the first list in buckets is already sorted and the other lists in buckets are all copies of it. 运行代码时,我总是发现存储桶中的第一个列表已经排序,存储桶中的其他列表都是它的副本。 Do I need the insertion sort for each list? 每个列表都需要插入排序吗? What would I use the "answer" variable for?! 我将“ answer”变量用于什么?

I'm mainly relying on this visualization . 我主要依靠这种可视化

One thing that i notice right off the bat is that you initialize your variable buckets as buckets = [[]] * len(aList) . 我立即注意到的一件事是,将变量存储buckets = [[]] * len(aList)初始化为buckets = [[]] * len(aList) This makes a list of identical copies of the empty list. 这将使空白列表的副本相同。 As such, any modification of this list is replicated in every element of buckets . 这样,此列表的任何修改都会复制到buckets每个元素中。 Change this line to: 将此行更改为:

buckets =  [[] for _ in xrange(len(aList))]

To check if the lists inside the list are separate object, you could check their id's: 要检查列表中的列表是否为单独的对象,可以检查其ID:

print [id(x) for x in buckets]

This should print a list of unique numbers. 这应该打印一个唯一编号列表。

I think this bucket sort would be more efficient and is more pythonesque. 我认为这种存储桶排序将更加高​​效,并且更具Python风格。

def bucket(k):
    unique = list(set(k))
    values = [k.count(uni) for uni in unique]
    result = ([unique[uni] for i in range(values[uni])] for uni in range(len(unique)))
    result = sum(result, [])
    return result

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

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