简体   繁体   English

对唯一的有界数组进行排序的最有效方法?

[英]Most efficient way to sort a unique, bounded array?

I assume this is a duplicate but I couldn't find one.我认为这是重复的,但我找不到。

Let's say we have an array where we know every single value will be unique, and every value will be between a range.假设我们有一个数组,我们知道每个值都是唯一的,并且每个值都在一个范围之间。 For example, let's say we know the array consists of unique values between 0 and 100. Example:例如,假设我们知道数组由 0 到 100 之间的唯一值组成。示例:

[ 5, 46, 15, 83, 55, 28 ]

I would like to then sort this array into the following:然后我想将此数组排序为以下内容:

[ 5, 15, 28, 46, 55, 83 ]

Obviously I could just use Quicksort or any other comparative sort, but that would be O(n log n) .显然我可以使用 Quicksort 或任何其他比较排序,但那将是O(n log n) Non-comparative sorts exist as well, such as Radix Sort, Bucket Sort, Counting Sort, etc. but none of those also maintain the constraint that values must be unique, as far as I know.也存在非比较排序,例如基数排序、桶排序、计数排序等,但据我所知,这些排序都没有保持值必须唯一的约束。

Generally it is the case that the more constraints you allow, the more efficient your algorithm can become.通常情况下,您允许的约束越多,您的算法就越有效。 Are there any algorithms which are even more efficient for unique, bounded arrays?是否有任何算法对独特的有界 arrays 更有效? If not, which of the non-comparative sorting algorithms would be most efficient for this type of dataset?如果不是,哪种非比较排序算法对这种类型的数据集最有效?

Given the constraint of unique values and a restricted range, you can declare an array whose length is enough to hold every unique integer in the given range, then you can perform a special-case bucket sort where each bucket is always able to contain a unique value, and finally filter the array to remove the leftover empty slots.鉴于唯一值的约束和限制范围,您可以声明一个数组,其长度足以容纳给定范围内的每个唯一 integer,然后您可以执行特殊情况的桶排序,其中每个桶总是能够包含唯一的值,最后过滤数组以删除剩余的空槽。 Like other non-comparative sorting algorithms, this approach is O(n) asymptotic time-complexity.与其他非比较排序算法一样,这种方法是 O(n) 渐近时间复杂度。

 const input = [5, 46, 15, 83, 55, 28]; function sort(unsorted, lower, upper) { const sorted = Array(upper - lower); for (let i = 0; i < unsorted.length; ++i) { sorted[unsorted[i] - lower] = unsorted[i]; } return sorted.filter(() => true); } const output = sort(input, 0, 100); console.log(output);

something like that?类似的东西?

 const arr1 = [ 5, 46, 15, 83, 55, 28 ] const min = Math.min(...arr1) const res = [] for(let v of arr1) res[v-min] = v for(let i=res.length;i--;) if (res[i]===undefined) res.splice(i, 1) console.log( res )
 .as-console-wrapper { max-height: 100%;important: top; 0; }

I would suggest a counting sort, but one using an array of bytes for what would normally be the counts, since the count values will only be 0 or 1 since the array to be sorted has unique values.我建议进行计数排序,但是使用一个字节数组作为通常的计数,因为计数值只会是 0 或 1,因为要排序的数组具有唯一值。 This would also work for an array where no value occurs more than 255 times (so that each count would still fit in an unsigned byte).这也适用于没有值出现超过 255 次的数组(因此每个计数仍然适合无符号字节)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM