I'm attempting to create an algorithm that can sort an array of integers in O(N) time.
我试图创建一个算法,可以在O(N)时间内对整数数组进行排序。
- The number of digits in all of the intergers is N
- 所有格中的位数都是N
- Each element has an unknown number of digits
- 每个元素都有未知的数字
- The algorithm should sort the array in O(N) time regardless of how the digits are distributed
- 算法应该在O(N)时间内对数组进行排序,而不考虑数字的分布
I have a working solution for this problem, that runs in O(N) time, I'm just having trouble trying to prove that it does so.
对于这个问题,我有一个可行的解决方案,它在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
The solution that I have come up with is that the ∑k*∑n
will always equal N.
我想出了解决方案是,∑k *∑n总是等于n。
Attempt at a proof
试图证明
Base case: Array has 1 item.
T(N)= k*1. k=N = O(N)
I'm unsure how to do the inductive step (if it is even required).
我不知道如何做归纳步骤(如果需要的话)。
1 个解决方案
#1
2
The following screenshot explains it:
下面的截图解释了这一点:
#1
2
The following screenshot explains it:
下面的截图解释了这一点: