我怎样才能字典顺序排序数字?字典、顺序、数字

2023-09-11 01:56:09 作者:回頭已無路

下面是该方案。

我给出一个整数数组A。该数组的大小不固定。那我应该写的函数可以用短短整数数组调用一次,而其他时间,它甚至可能包含数千个整数。此外,每个整数不必含有的位数相同数目的

I am given an array 'A' of integers. The size of the array is not fixed. The function that I am supposed to write may be called once with an array of just a few integers while another time, it might even contain thousands of integers. Additionally, each integer need not contain the same number of digits.

我应该'排序'数组中的数字,使得产生的阵列已经下令在词典的方式(即他们都是根据自己的字符串重新presentations来分类的。这里的123是字符串的整数重123 presentation)。请注意,输出应该只包含整数,而不是他们的字符串等效。

I am supposed to 'sort' the numbers in the array such that the resulting array has the integers ordered in a lexicographic manner (i.e they are sorted based on their string representations. Here "123" is the string representation of 123). Please note that the output should contain integers only, not their string equivalents.

例如:如果输入的是:

[12 | 2434 | 23 | 1 | 654 | 222 | 56 | 100000]

[ 12 | 2434 | 23 | 1 | 654 | 222 | 56 | 100000 ]

那么输出应该是:

[1 | 100000 | 12 | 222 | 23 | 2434 | 56 | 654]

[ 1 | 100000 | 12 | 222 | 23 | 2434 | 56 | 654 ]

我最初的方法:我转换的每个整数的字符串格式,然后加零,以它让所有的整数包含相同位数的(这是凌乱的一步,因为它涉及到的跟踪权等使该解决方案非常低效的),然后做基数排序。 最后,我删除了填零,转换成字符串回到他们的整数,并把它们产生的数组中开始。这是一个非常低效的解决方案。

My initial approach: I converted each integer to its string format, then added zeros to its right to make all the integers contain the same number of digits (this was the messy step as it involved tracking etc making the solution very inefficient) and then did radix sort. Finally, I removed the padded zeros, converted the strings back to their integers and put them in the resulting array. This was a very inefficient solution.

我一直认为,导致该解决方案不需要填充等,并有一个简单的解决方案,你只需要处理数以某种方式(一些位处理?)得到的结果。

I've been led to believe that the solution doesn't need padding etc and there is a simple solution where you just have to process the numbers in some way (some bit processing?) to get the result.

什么是空间,聪明的最有效的解决方案,你能想到什么?时间方面?

What is the space-wise most efficient solution you can think of? Time-wise?

如果你给code,我想preFER Java或伪code。但是,如果不适合你,任何这样的语言应该是不错。

If you are giving code, I'd prefer Java or pseudo-code. But if that doesn't suit you, any such language should be fine.

推荐答案

可执行伪code(又名Python中): thenumbers.sort(键= STR) 。是的,我知道,使用Python是一种像作弊 - 它只是太强大;-)。但严重的是,这也意味着:如果你能排序字符串数组字典顺序,如Python的排序本质可以,那么就使钥匙串出每个数字和那种辅助阵列(然后你可以重建所需的数字数组一个STR-> INT改造,或通过间接做排序的指标,等等等等);这被称为DSU(装饰,排序,去除装饰),它是什么键= 参数Python的排序工具。

Executable pseudo-code (aka Python): thenumbers.sort(key=str). Yeah, I know that using Python is kind of like cheating -- it's just too powerful;-). But seriously, this also means: if you can sort an array of strings lexicographically, as Python's sort intrinsically can, then just make the "key string" out of each number and sort that auxiliary array (you can then reconstruct the desired numbers array by a str->int transformation, or by doing the sort on the indices via indirection, etc etc); this is known as DSU (Decorate, Sort, Undecorate) and it's what the key= argument to Python's sort implements.

在更详细的(伪code):

In more detail (pseudocode):

分配字符** 辅助只要数字阵列的数组 为我从0到的长度的数字-1 AUX [I] =字符串化(编号[I]) 分配INT 指数的长度相同的数组 为我从0到的长度的数字-1 指数[我] =我 排序指数,使用如 CMP(I,J) STRCMP(AUX [我],辅助[J]。) 分配INT 结果的长度相同的数组 为我从0到数字-1 ,长度的结果[I] =号[指数[I]] 的memcpy 结果数字 免费每 AUX [I] ,也辅助指数结果 allocate an array of char** aux as long as the numbers array for i from 0 to length of numbers-1, aux[i]=stringify(numbers[i]) allocate an array of int indices of the same length for i from 0 to length of numbers-1, indices[i]=i sort indices, using as cmp(i,j) strcmp(aux[i],aux[j]) allocate an array of int results of the same length for i from 0 to length of numbers-1, results[i]=numbers[indices[i]] memcpy results over numbers free every aux[i], and also aux, indices, results