【问题标题】:Sorting an array of integers from 0...n, with n digits, in O(n) time在 O(n) 时间内从 0...n 对具有 n 个数字的整数数组进行排序
【发布时间】:2012-01-31 16:12:19
【问题描述】:

我正在尝试创建一种算法,可以在 O(N) 时间内对整数数组进行排序。

  • 所有整数的位数为 N
  • 每个元素的位数未知
  • 无论数字如何分布,算法都应该在 O(N) 时间内对数组进行排序

我有一个解决这个问题的有效解决方案,它在 O(N) 时间内运行,我只是在试图证明它这样做时遇到了麻烦。

Create a set of N buckets and add items to their corresponding bucket based off how
many digits are in the integer -O(N)

Radix sort each bucket, and then concatenate the buckets back together. 
Sum k=0 to N of O(k*n)
k = Number of digits
n = number of items with k digits

我想出的解决方案是∑k*∑n 总是等于 N。

尝试证明

Base case: Array has 1 item.
T(N)= k*1. k=N = O(N)

我不确定如何进行归纳步骤(如果需要的话)。

【问题讨论】:

  • 您的基数排序想法可能比您想象的要昂贵。例如。 N=4,数组 = [1,23,456,7890]
  • @ElKamina,在您的示例中,删除了结尾 0,n=9

标签: algorithm sorting big-o


【解决方案1】:

下面的截图解释了它:

【讨论】:

  • 谢谢,这帮我弄明白了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-24
  • 1970-01-01
  • 1970-01-01
  • 2021-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多