简体   繁体   English

如何确定Bucket Sort的平均和最差情况空间复杂度

[英]How to determine the Average and Worst Case Space Complexity of Bucket Sort

有人能说出如何找到Bucket排序的平均和最差情况SPACE复杂度吗?

Well first off we need to understand how bucket sort works: 首先,我们需要了解桶排序的工作原理:

  1. Create an empty array 创建一个空数组
  2. Loop through the original array and put each object in a bucket 循环遍历原始数组并将每个对象放入存储桶中
  3. Sort each of the non-empty buckets 对每个非空桶进行排序
  4. Check the buckets in order and then put all objects back into the original array. 按顺序检查存储桶,然后将所有对象放回原始数组中。

This makes bucket sort a great algorithm for big lists that need to be sorted. 这使得bucket对于需要排序的大型列表来说是一种很好的算法。 The average time complexity is O(n+k) where n is the number of your buckets. 平均时间复杂度为O(n + k),其中n是您的桶的数量。 The worst time complexity is Θ(n^2). 最差的时间复杂度是Θ(n ^ 2)。 The reason for that is because bucket sort is useful when input is uniformly distributed over a range since whenever there are keys that are close to each other they are probably going to end up in the same bucket otherwise we would need a bucket for each key in the original array. 原因是因为当输入在一个范围内均匀分布时,桶排序很有用,因为每当有相互靠近的键时,它们可能最终会在同一个桶中结束,否则我们需要一个桶用于每个键。原始数组。

The worst case space complexity is O(nk). 最坏的情况是空间复杂度为O(nk)。 Space complexity is a measure of the amount of working storage an algorithm needs. 空间复杂度是算法所需的工作存储量的度量。 That means how much memory, in the worst case, is needed at any point in the algorithm. 这意味着在最坏的情况下,算法中的任何一点都需要多少内存。 As with time complexity, we're mostly concerned with how the space needs grow, in big-Oh terms, as the size N of the input problem grows. 与时间复杂性一样,我们主要关注的是空间需求如何增长,就像输入问题的大小N增长一样。

I hope that helped! 我希望有所帮助!

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

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