[英]Radix sort algorithm with O(n) worst case
Let's say that you are given an integer array A
of size n
.假设您有一个大小为
n
的 integer 数组A
。 You know in advance that O(√n)
elements of A
can be larger than 2020(n)^(5) − 5n
, and the remaining elements of A are in the range [1, 2020n^5 − 5n]
.你事先知道
A
的O(√n)
个元素可以大于2020(n)^(5) − 5n
,而 A 的其余元素在[1, 2020n^5 − 5n]
范围内。 It turns out that, in this case, A
can be sorted in O(n)
time in the worst case.事实证明,在这种情况下,在最坏的情况下,
A
可以在O(n)
时间内排序。
I am trying to solve this interesting algorithm question and my intuition is to use radix sort as part of my solution.我正在尝试解决这个有趣的算法问题,我的直觉是使用基数排序作为我的解决方案的一部分。 The part that stumps me is the
O(√n)
runtime, so any pointers in finding such an algorithm would be greatly appreciated!难倒我的部分是
O(√n)
运行时,所以任何找到这种算法的指针都将不胜感激!
Separate the in-range elements from the out-of-range elements (O(n)).将范围内元素与范围外元素分开 (O(n))。 Radix sort in the in-range elements (base n; this takes six passes for n ≥ 2020 and is O(n)).
在范围内的元素中进行基数排序(以 n 为底;对于 n ≥ 2020 需要六次遍历,并且为 O(n))。 Insertion sort the out-of-range elements (there are √n of these, hence O(√n²) = O(n)).
插入对超出范围的元素进行排序(其中有 √n 个,因此 O(√n²) = O(n))。 Merge the two sorted arrays (O(n)).
合并两个排序后的 arrays (O(n))。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.