简体   繁体   English

计数排序O(n + k)时间复杂度中的k是多少?

[英]What is k in counting sort O(n+k) time complexity?

Counting sort worst, best and average time complexity is O(n+k) , where n is number of elements to sort. 计算排序的最差,最佳和平均时间复杂度为O(n+k) ,其中n是要排序的元素数。 What is k exactly? k是多少? I see different definitions: maximum element, difference between max element and min element, and so on. 我看到了不同的定义:最大元素,最大元素和最小元素之间的差异,等等。

  1. Given array arr1 [1, 3, 5, 9, 12, 7 ] and arr2 [1,2,3,2,1,2,4,1,3,2] what is k for arr1 and arr2 ? 给定数组arr1 [1, 3, 5, 9, 12, 7 ] arr2 [1,2,3,2,1,2,4,1,3,2]arr2 [1,2,3,2,1,2,4,1,3,2]对于arr1arr2什么是k
  2. Is it true that it is stupid to sort arr1 with counting sort because n < k (element values are from a range which is wider than number of elements to sort? 是因为n < k (元素值的范围大于要排序的元素数的范围),所以用计数排序对arr1进行排序是愚蠢的吗?

k is the range of the keys, ie the number of array slots it takes to cover all possible values. k是键的范围,即覆盖所有可能值所需的阵列插槽数。 Thus in case of numbers, Max-Min+1 . 因此,如果是数字, Max-Min+1 Of course this assumes that you don't waste space by assigning Min the first slot and Max the last. 当然,这假设您没有通过将Min分配给第一个插槽,将Max分配给最后一个插槽来浪费空间。

It is appropriate to use counting sort when k does not exceed a small multiple of n , let nk , as in this case, nk can beat n.log n . k不超过n的小倍数时,最好使用计数排序,让nk n.log n ,因为在这种情况下nk可以击败n.log n

k是数组中的最大可能值,假设您有长度为5的数组,每个数字都是0到9之间的整数,在此示例中,k等于9

First an array of k counts is zeroed. 首先,将k个计数的数组清零。 Then the n elements in the array are read, and the elements of the k counts are incremented depending on the values of the n elements. 然后读取数组中的n个元素,并根据n个元素的值增加k个计数的元素。 On the output pass of a counting sort, the array of k counts is read, and array of n elements is written. 在计数排序的输出通道上,读取k个计数的数组,并写入n个元素的数组。 So there are k writes (to zero the counts), n reads, then k reads and n writes for a total of 2n + 2k operations, but big O ignores the constant 2, so the time complexity is O(n + k). 因此,共有k次写入(计数为零),n次读取,k次读取和n次写入,总共进行2n + 2k次操作,但是大O忽略常数2,因此时间复杂度为O(n + k)。

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

相关问题 Bucket Sort O(n+k) 如果使用插入排序对每个桶进行排序,时间复杂度如何? - How is the time complexity of Bucket Sort O(n+k) if it uses insertion sort to sort each bucket? 铲斗分类的复杂程度如何才是O(n + k)? - How could the complexity of bucket sort is O(n+k)? O(nk)和O(n + k)在时间复杂度上有什么区别? - what is the difference between O(nk) and O(n+k) in time complexity? 如果我们使用链表实现存储桶,那么存储桶排序的复杂性如何是O(n + k)? - How is the complexity of bucket sort is O(n+k) if we implement buckets using linked lists? 查找特定的缺失数组元素:O(n + k)复杂度 - Finding a Particular Missing Array Element: O(n+k) Complexity 字符串切片的时间复杂度是多少? O(k) 或 O(n) - What is the time complexity of string slice? O(k) or O(n) 代码段的时间复杂度是否为O(k * n ^ 2)? - Is the time complexity of the code snippet O(k*n^2)? 为什么基数排序的空间复杂度为O(k + n)? - Why does radix sort have a space complexity of O(k + n)? 在O(nk)时间复杂度中合并k个排序大小为n的数组 - Merge k sorted arrays of size n in O(nk) time complexity 是否有可能从时间复杂度为O(n)和空间复杂度为O(k)的n个未排序整数中找到k个最大的数? - Is it possible to find the k-largest numbers from n unsorted integers with time complexity O(n) and space complexity O(k)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM