简体   繁体   English

O(n) 最坏情况的基数排序算法

[英]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] .你事先知道AO(√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.

相关问题 冒泡排序最坏的例子是O(n * n),怎么样? - Bubble sort worst case example is O(n*n), how? O(n)最坏情况时间的2D峰值发现算法? - 2D peak finding algorithm in O(n) worst case time? 为什么这个算法的最坏情况时间复杂度是 O(n)? - why is the worst case time complexity of this algorithm O(n)? 用于进行k选择的最坏情况O(n)算法 - Worst-case O(n) algorithm for doing k-selection 最坏情况时间复杂度为O(n)的算法总是比最坏情况时间复杂度为O(n ^ 2)的算法快吗? - Is an algorithm with a worst-case time complexity of O(n) always faster than an algorithm with a worst-case time complexity of O(n^2)? 基数排序-O(n)时间 - Radix Sort - O(n) Time 比较排序算法中Ω(n logk)最坏情况复杂度的证明 - Proof of Ω(n logk) worst case complexity in a comparison sort algorithm 数组中不同整数的数量为O(log n)。 如何获得O(n log log n)最坏情况时间算法来对此类序列进行排序? - The number of distinct integers in an array is O(log n). How to get an O(n log log n) worst-case time algorithm to sort such sequences? 排序为O(n * log(n))最坏的情况 - Sorting in O(n*log(n)) worst case 为什么合并排序的最坏情况运行时间为 O (n log n)? - Why is merge sort worst case run time O (n log n)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM