简体   繁体   English

在线性时间内对 0 到 n^2 – 1 范围内的 n 个数字进行排序

[英]Sorting n numbers in range from 0 to n^2 – 1 in linear time

How the elements of an array got sorted by using counting sort two times??如何使用两次计数排序对数组的元素进行排序?

Is it fixed that there would be the use of counting sort exact two times?是否会使用两次精确计数排序?

I know it is related to radix sort (which is the subroutine of counting sort) in which elements are sorted by considering each digit at a time.我知道它与基数排序(这是计数排序的子程序)有关,其中元素通过一次考虑每个数字进行排序。

for more detail:- https://www.geeksforgeeks.org/sort-n-numbers-range-0-n2-1-linear-time/更多细节:- https://www.geeksforgeeks.org/sort-n-numbers-range-0-n2-1-linear-time/

(please don't declare it as a duplicate I have already checked posts related to this.) (请不要将其声明为重复我已经检查过与此相关的帖子。)

How will the elements be of two-digit after converting the numbers to base n?将数字转换为基数 n 后,元素将如何变为两位数? Can you tell me this, please?请告诉我这个好吗?

Thanks in advance.提前致谢。

The trick described in the article is to consider the elements of the array as 2-digit numbers in base n, and them sort them using two passes of counting sort.文章中描述的技巧是将数组的元素视为以 n 为底的 2 位数字,并使用两次计数排序对它们进行排序。

The idea is that sorting the numbers by the bottom digit (using a stable sorting algorithm), and then sorting the numbers by the higher digit (using the same stable sorting algorithm) results in the numbers being sorted.这个想法是按底部数字对数字进行排序(使用稳定的排序算法),然后按较高的数字对数字进行排序(使用相同的稳定排序算法)导致数字被排序。 These two steps can be considered a form of radix sort except rather than sorting by bit, it's sorting by digits base n.这两个步骤可以被认为是基数排序的一种形式,除了它不是按位排序,而是按以 n 为底的数字排序。

Given that the digits are all in the range 0..n-1, it's possible to use counting sort to do each of the two sorting steps in linear time.鉴于数字都在 0..n-1 范围内,可以使用计数排序在线性时间内完成两个排序步骤中的每一个。 There's some fiddly details to get right so that the counting sort is stable, but those details (and code) are provided in the article.为了使计数排序稳定,需要处理一些繁琐的细节,但文章中提供了这些细节(和代码)。

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

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