简体   繁体   English

荷兰国旗-演绎解决方案

[英]Dutch national flag - Deducing solution

After learning elementary sorts, selection sort , insertion sort & heap sort from sedgewick course in Coursera. 从Coursera的sedgewick 课程学习基本排序, 选择排序插入排序堆排序之后。

Selection sort - final sorted order starts forming every iteration 选择排序 -最终排序的顺序开始形成每个迭代

Insertion sort - Ascending order(not final) starts forming every iteration. 插入排序 -升序(不是最终的)开始形成每个迭代。

Heap sort - Aggressive version of insertion sort. 堆排序 -插入排序的激进版本。


I have an assignment, in the above mentioned course, after learning sort algorithms(shown above), 在学习了排序算法(如上所示)之后,我在上述课程中分配了一个作业,

Dutch national flag . 荷兰国旗 Given an array of n buckets, each containing a red, white, or blue pebble, sort them by color. 给定n存储桶的阵列,每个存储桶包含一个红色,白色或蓝色的卵石,请按颜色对其进行排序。 The allowed operations are: 允许的操作是:

swap(i,j) : swap the pebble in bucket i with the pebble in bucket j . swap(i,j)swap存储桶i中的卵石与存储桶j的卵石swap

color(i) : color of pebble in bucket i . color(i) :桶i中卵石的颜色。

The performance requirements are as follows: 性能要求如下:

At most n calls to color() . 最多n调用color()

At most n calls to swap() . 最多n调用swap()

Constant extra space. 恒定的额外空间。


By reading this question, 通过阅读此问题,

From the performance requirements, given, I get a hint that n swaps are required(at-most). 从给出的性能要求中,我得到一个提示,要求(最多)进行n交换。 From this hint, I think about, 从这个暗示,我想,

1) Insertion sort that ensures n swaps for n inversion pairs. 1)插入排序,确保对n个反转对进行n次交换。

2) Am assuming colors that are stored in buckets, there is very great chance that, similar colors sit closer, given n buckets. 2)假设存储在存储桶中的颜色,则在给定n个存储桶的情况下,很有可能出现相似的颜色。 This leads to a situation of partially sorted array, where an array is partially sorted, if the number of inversions <= cn . 如果反转数<= cn ,则会导致部分排序数组的情况,其中对数组进行部分排序。

Based on these above two conclusions, 基于以上两个结论,

Question: 题:

1) 1)

Is the thought process correct in deducing to insertion sort, as solution? 作为推论插入排序的思维过程是否正确,作为解决方案?

2) 2)

What is the hash function to hash colors and insert colors in as partially sorted array? 什么是哈希函数来哈希颜色并将颜色插入为部分排序的数组? Do I need linear probing method? 我需要线性探测方法吗?

3) Why do we need extra space, as given in question? 3)为什么我们需要额外的空间? Because Insertion sort is in-place algorithm 因为插入排序是就地算法

Note: To confirm my thought process, before writing code 注意:在编写代码之前,请先确认我的思考过程

1) Insertion sort that ensures n swaps for n inversion pairs. 1)插入排序,确保对n个反转对进行n次交换。

2) Am assuming colors that are stored in buckets, there is very great chance that, similar colors sit closer, given n buckets. 2)假设存储在存储桶中的颜色,则在给定n个存储桶的情况下,很有可能出现相似的颜色。 This leads to a situation of partially sorted array, where an array is partially sorted, if the number of inversions <= cn . 如果反转数<= cn ,则会导致部分排序数组的情况,其中对数组进行部分排序。

There are two problems with this line of reasoning. 这种推理有两个问题。

The first is that the number of inversions is not O( n ) in the worst case. 首先是最坏情况下的反转次数不是O( n )。 To see why, imagine that you have n / 2 blue pebbles followed by n / 2 red pebbles (and no white pebbles). 要了解为什么,请想象您有n / 2个蓝色卵石,然后是n / 2个红色卵石(并且没有白色卵石)。 Then the total number of inversions is ( n 2 ) / 4 . 那么反演的总数为n 2 / 4 (And furthermore, judging from some quick experiments I've just run, even the average-case total number of inversions is ( n 2 ) / 6 — still not O( n ).) (此外,从我刚刚进行的一些快速实验来看,即使平均情况下的求反总数为n 2 / 6-仍然不是O( n )。)

The second problem with this line of reasoning is that you're asked to limit your number of swaps to exactly n , not O( n ). 这条推理的第二个问题是,要求您将交换次数限制为正好n ,而不是O( n )。 So even if insertion sort only required (say) 2n swaps, that would still be too many. 所以,即使插入排序,才需要(说)2个N互换,这将仍然是太多了。


2) [¶] What is the hash function to hash colors and insert colors in as partially sorted array? 2)[¶]什么是哈希函数来哈希颜色并将颜色插入为部分排序的数组? Do I need linear probing method? 我需要线性探测方法吗?

Insertion sort does not require a hash function; 插入排序不需要哈希函数; and I don't think you can build any meaningful sort of hash table within your constraint of constant additional space. 而且我认为您无法在恒定的额外空间约束内构建任何有意义的哈希表。

Rather, you can just represent the three colors as (say) 0, 1, and 2. 相反,您可以仅将三种颜色表示为(例如)0、1和2。

3) Why do we need extra space, as given in question? 3)为什么我们需要额外的空间? Because Insertion sort is in-place algorithm 因为插入排序是就地算法

Even insertion sort requires some extra variables, such as array-indices. 甚至插入排序也需要一些额外的变量,例如数组索引。


Alexander Anikin has already suggested the same solution that I was going to suggest, but I'm not sure if it's clear from his answer how you could come up with that on your own, so the rest of this answer will walk you through that. Alexander Anikin已经提出了我将要建议的相同解决方案,但是我不确定他的答案是否清楚您可以自己提出该解决方案,因此本答案的其余部分将带您逐步解决。


So, suppose you start by trying insertion sort. 因此,假设您先尝试插入排序。 We can then improve it incrementally until it satisfies the requirements. 然后,我们可以逐步改进它,直到满足要求。 (This does require having a few insights, but each individual improvement requires only a small insight, rather than having to solve the whole problem at once.) (这确实需要有一些见识,但是每个单独的改进都只需要一个小的见解,而不必立即解决整个问题。)

First improvement: # of calls to color(i) . 第一个改进:对color(i)的调用次数。

When we want to insert a new pebble into the already-sorted portion of the array, we need to find where it should go in that portion. 当我们想将一个新的小卵石插入到数组已排序的部分时,我们需要找到它在该部分中的位置。

With pure insertion sort, we do that by examining pebbles in the already-sorted portion of the array. 对于纯插入排序,我们通过检查数组已排序部分中的卵石来实现。 But we're only allowed to call color exactly n times; 但是我们只能将color精确地调用n次。 and we expect to have to call it at least once for each pebble — you can't sort a pebble without examining it — which means that to make this work, we need to call it exactly once for each pebble. 而且我们希望每个卵石至少要调用一次-如果不检查卵石就不能对其进行分类-这意味着要进行这项工作,我们需要为每个卵石恰好调用一次。 We can't afford to re-call it for a pebble that we've already sorted. 我们不能为已经分类的卵石调用它。

To fix this, the key insight is that in the already-sorted part of the array, we don't actually need to examine a bucket in order to know what color pebble it has, provided we've kept track of how many pebbles we've already seen of each color. 为了解决这个问题,关键的见解是,在数组的已排序部分中,只要我们一直跟踪我们有多少个卵石,我们实际上就不需要检查一个桶来知道它具有什么颜色的卵石。已经见过每种颜色。 If we've previously seen r red pebbles, w white pebbles, and b blue pebbles, then the red pebbles are in buckets 0 through r-1 , the white ones are in r through r+w-1 , and the blue ones are in buckets r+w through r+w+b-1 . 如果我们以前看过r红色小卵石, w白色小卵石和b蓝色小卵石,则红色小卵石在桶0r-1 ,白色在rr+w-1桶中,蓝色是在r+wr+w+b-1桶中。 So we can figure out where to insert a new pebble without having to actually call color on any other pebbles. 因此,我们可以找出在哪里插入新的卵石,而不必在任何其他卵石上实际调用color

Second improvement: # of calls to swap(i,j) , part 1. 第二个改进:对swap(i,j)的调用次数,第1部分。

The next problem is that with pure insertion sort, inserting an pebble means "shifting" all the pebbles after it, which potentially means a large number of swaps. 下一个问题是,对于纯插入排序,插入小卵石意味着将其后的所有小卵石“移位”,这可能意味着大量交换。 (For example, if we've already seen 10 white pebbles and 5 blue pebbles, then inserting a red pebble means "shifting" 15 pebbles, which requires 15 swaps.) (例如,如果我们已经看到10个白色小卵石和5个蓝色小卵石,那么插入一个红色小卵石意味着“移动” 15个小卵石,这需要进行15次交换。)

To fix this, the key insight is that we don't actually care about the order of the pebbles within a color-group; 为了解决这个问题,关键的见解是我们实际上并不关心颜色组中小卵石的顺序。 the red pebbles can come in any order, followed by the white pebbles in any order, followed by the blue pebbles in any order. 红色小卵石可以按任何顺序排列,然后是白色小卵石,可以按任意顺序排列,然后是蓝色小卵石,可以按任意顺序排列。 So, rather than shifting a large number of pebbles by just one position, we can shift a single pebble from the start of a color-group to the end of it. 因此,我们可以将一个小卵石从一个颜色组的开始移到它的末尾,而不是将大量小卵石仅移一个位置。

For example, if we've previously seen r red pebbles, w white pebbles, and b blue pebbles, and we need to insert a new red pebble, then we can swap it into position r (giving us a white pebble (if w > 0 )), then swap the white pebble into position r+w (giving us a blue pebble (if b > 0 )). 例如,如果我们之前看过r红色小卵石, w白色小卵石和b蓝色小卵石,并且需要插入一个新的红色小卵石,则可以将其交换到位置r (给我们一个白色小卵石(如果w > 0 )),然后将白色小卵石交换到位置r+w (为我们提供蓝色小卵石(如果b > 0 ))。

This brings us down to O( n ) swaps in the worst case, and just under n swaps in the average case: a red pebble will require two swaps (if w > 0 and b > 0 ), a white pebble will require one swap (if b > 0 ), and a blue pebble will require zero swaps. 在最坏的情况下,这使我们陷入O( n )交换,而在一般情况下,则接近n n交换:一个红色的卵石将需要两次交换(如果w > 0b > 0 ),一个白色的卵石将需要一次交换(如果b > 0 ),则蓝色小卵石将需要零交换。

Third improvement: # of calls to swap(i,j) , part 2. 第三处改进:调用swap(i,j)的电话号码,第2部分。

But this still leaves us with potentially too many swaps: if there are more red pebbles than blue ones, then we'll (usually) end up with more than n swaps. 但这仍然给我们带来了太多的交换:如果红色小卵石多于蓝色小卵石,那么(通常)我们最终将获得n个以上的交换。

To fix this, we need one last insight, which is that we know where blue pebbles will end up (since we don't care about the order they end up); 要解决此问题,我们需要获得最后的见解,那就是我们知道蓝色卵石的最终位置(因为我们不在乎它们的最终顺序)。 so instead of putting blue pebbles immediately after the end of the already-seen white pebbles, we can put blue pebbles at the end of the array. 因此,我们可以将蓝色小卵石放置在数组的末尾 ,而不是在已经看到的白色小卵石结束后立即放置蓝色小卵石。 That way we don't need to keep re-shifting the range of blue pebbles; 这样一来,我们就无需继续移动蓝色卵石的范围。 once a blue pebble is placed, it can stay put. 一旦放置了蓝色卵石,它就可以保持原状。

So, for example, if we've seen r red pebbles, w white pebbles, and b blue pebbles, then the red pebbles should be in positions 0 through r-1 , the white pebbles should be in positions r through r+w-1 , and the blue pebbles should be in positions nb through n-1 . 因此,例如,如果我们看到r红色小卵石, w白色小卵石和b蓝色小卵石,则红色小卵石应该位于0r-1位置,白色小卵石应该位于rr+w-1 ,并且蓝色小卵石应位于位置nbn-1 The positions from r+w through nb-1 are the as-yet-unsorted range. r+wnb-1位置尚未分类。


So, our final algorithm looks like this: 因此,我们的最终算法如下所示:

  • initialize r , w , and b to 0 . rwb初始化为0
  • initialize i to 0 . i初始化为0
  • while i < n - b : i < n - b
    • check color(i) : 检查color(i)
      • if it's RED, then swap(i, r) and increment r and i . 如果是红色,则swap(i, r)并递增ri
      • if it's WHITE, then just increment w and i . 如果是白色,则只需增加wi
      • if it's BLUE, then swap(i, nb-1) and increment b . 如果是蓝色,则swap(i, nb-1)并递增b Do not increment i (because the pebble in position i is one that we just pulled from near the end of the array, that we haven't examined yet). 增加i (因为位置的卵石i是一个我们刚刚从附近的阵列的端侧拉拽,我们还没有研究)。

(Note that a few swaps can be eliminated by double-checking whether the two positions being swapped are actually different; but, that's not needed. We're under n swaps either way.) (请注意,可以通过仔细检查两个被互换的头寸是否实际上不同来消除一些互换;但这不是必需的。我们都处于n个互换之下。)


Now, what if we had started with a different sort, rather than insertion sort? 现在,如果我们从其他排序而不是插入排序开始呢? Some kinds of sorts would still take us in the right direction; 某些类型的问题仍然会带我们朝正确的方向前进; for example, if we started with a bucket sort (or a sort of radix sort with only a single digit 0-2), we could still have made incremental fixes to end up at the above algorithm. 例如,如果我们从存储桶排序(或只有一个数字0-2的基数排序)开始,我们仍然可以进行增量修复以结束上述算法。 Other kinds of sorts would not have worked out so well, but still might have led to useful insights that would have helped once we got on a better path. 其他种类的解决方案效果不佳,但可能仍会带来有用的见解,一旦我们走上更好的道路,这些见解将有所帮助。 So it's important to try different things and keep an open mind. 因此,尝试不同的事情并保持开放的态度很重要。 The more you explore a problem, the more insights you get, even if you occasionally go down the wrong path and need to back out and try something new. 即使您偶尔走错路并需要退出尝试新的事物,您对问题的探索越多,就可以获得更多的见解。

To elaborate solution for this problem, you don't need mentioned algorithms. 要详细说明该问题的解决方案,您无需提及算法。

Instead become familiar with partition routine implementations of Quick Sort - it would really helpful. 而是熟悉快速排序的分区例程实现-确实会有所帮助。

PS 聚苯乙烯

Heap sort - Aggressive version of insertion sort 堆排序-插入排序的激进版本

No, you are wrong, it is completely different approach 不,你错了,这是完全不同的方法

You can consider how Shell sort does exploit Insert sort internally 您可以考虑Shell排序如何在内部利用插入排序

At first glance it looks like introduction to bucket sort: move every red pebble into Reds array, move every white pebble into Whites array, move every blue pebble into Blues array, and then copy these 3 arrays into final output array. 乍一看,它看起来像是桶排序的介绍:将每个红色小卵石移动到Reds数组中,将每个白色小卵石移动到Whites数组中,将每个蓝色小卵石移动到Blues数组中,然后将这3个数组复制到最终输出数组中。 Naive implementation will take 3* n calls of color() , 2* n calls of swap() and n +constant additional memory. 天真的实现将需要3 * n次调用color() ,2 * n次调用swap()n +恒定的额外内存。

But restrictions are a bit off limits... We can not afford simple decisions, we can not afford additional memory for temporary arrays. 但是限制有点超出限制了……我们无法承受简单的决定,我们无法承受临时阵列的额外内存。

Note that n is the length of input array, not number of permutations and not multiplied by something. 请注意, n是输入数组的长度,而不是排列的数量并且不乘以某物。 That is: 那是:

  • each call of swap() should place one pebble at its final place 每个swap()调用应在其最终位置放置一个卵石
  • swaps should be done in-place 交换应该就地完成
  • each call of color() should lead to appropriate call of swap() 每个color()调用应导致对swap()的适当调用

It's possible in case of only 3 colors. 只有三种颜色时才有可能。 To make it possible we can divide input array to 3(4) sections: 为了使之成为可能,我们可以将输入数组划分为3(4)个部分:

r) red pebbles in the beginning of array r)数组开头的红色小卵石

w) white pebbles follow red pebbles from the beginning w)白色鹅卵石从一开始就跟随红色鹅卵石

u) unprocessed pebbles follow white pebbles up to blue pebbles u)未加工的卵石跟随白色卵石直至蓝色卵石

b) blue pebbles in the end of array b)数组末尾的蓝色小卵石

Let's assume we use 0-based indexing of arrays. 假设我们使用基于0的数组索引。 So, index a[0] is first element of array and a[n-1] is last element of array. 因此,索引a [0]是数组的第一个元素,而a [n-1]是数组的最后一个元素。

Now to keep track of array state we can use few variables: 现在,要跟踪数组状态,我们可以使用几个变量:

  • variable r will hold number of processed red pebbles 变量r将保存已处理的红色小卵石的数量

  • variable w will hold number of processed white pebbles 变量w将保存已处理的白色卵石的数量

  • variable b will hold number of processed blue pebbles 变量b将保存已处理的蓝色小卵石的数量

  • variable i will hold current index of pebble to process(processed) 变量i将保存要处理的小卵石的当前索引

This can be invariant for processing loop. 对于处理循环,这可以是不变的。 At start r=0, w=0, b=0, i=0. 在开始时r = 0,w = 0,b = 0,i = 0。 At end r, w, b hold number of corresponding pebbles and i=nb. 在端r,w,b处保持相应的小卵石的数量,并且i = nb。

So, array will look like this in the beginning: 因此,数组在开始时将如下所示:

uuuuuuuuuuuu
^           ^
r           n=n-0=n-b

Array will look like this during processing: 数组在处理过程中将如下所示:

rrwwwuuuuuuub
  ^  ^      ^
  r  w+r    n-b 
     i

Arrya will look like this in the end: 最后,Arrya看起来像这样:

rrrwwwwwwbbbb
^  ^     ^   ^
0  r     n-b n
         w+r
         i

Each step of processing loop (i is incremented after this) we make decision and do one of 3 possible things: 处理循环的每个步骤(此后i都会递增),我们进行决策并执行以下三种可能的操作之一:

  • if a[i] is red, we make swap(r, i) and increase r 如果a [i]是红色,我们使swap(r,i)并增加r
  • if a[i] is white, we increase w - it's useless action, actually, as we don't really use w, but I think it's better do this for execise 如果a [i]是白色,我们增加w-实际上是无用的动作,因为我们并不真正使用w,但是我认为这样做最好执行
  • if a[i] is blue, we increase b and make swap(i, nb) - we can skip swaps if i=nb, but generally algorithm should work without this skip 如果a [i]是蓝色,我们增加b并进行swap(i,nb)-如果i = nb,我们可以跳过交换,但是一般情况下算法应该工作而无需跳过

Loop finishes once i>=nb 一旦i> = nb,循环结束

Now, for your questions: 现在,对于您的问题:

Is the thought process correct in deducing to insertion sort, as solution? 作为推论插入排序的思维过程是否正确,作为解决方案?

Nope, insertion sort will do more than n calls of color() and more than n calls of swap() 不,插入排序将执行n次以上的color()调用和n次以上的swap()调用

What is the hash function to hash colors and insert colors in as partially sorted array? 什么是哈希函数来哈希颜色并将颜色插入为部分排序的数组?

You did not specify order of colors, so I've decided it myself. 您没有指定颜色顺序,所以我自己决定。 But you don't need hash for pebbles, since it will only increase number of swaps if you put pebbles into temporary data structure 但是您不需要为卵石散列,因为如果将卵石放入临时数据结构中,只会增加交换次数

Do I need linear probing method? 我需要线性探测方法吗?

It's tricky part - one call of color() per input pebble (no more than 1). 这是棘手的部分-每个输入小卵石一次调用color() (不超过1个)。 You can store color() result in local variable and then use local variable in, say, case operator. 您可以将color()结果存储在局部变量中,然后在例如case运算符中使用局部变量。 Strictly said, it is wrong - you still do more comparisions of color than needed, but probably your teacher will accept this approach, since you still call color() only that number of times. 严格地说,这是错误的-您仍然对颜色进行比所需的更多的比较,但是您的老师可能会接受这种方法,因为您仍然仅次调用color()。 Hash is not needed, really. 确实不需要哈希。

Still, you can try array or member function references if your programming language allows it, indexed by pebble colors. 不过,如果您的编程语言允许,则可以尝试使用数组或成员函数引用(由卵石颜色索引)。 Each function should do appropriate steps for processing loop decision as described above. 如上所述,每个功能都应执行适当的步骤来处理循环决策。

Since you did not specify PL and how colors are represented, it's hard to suggest how this can be done effectively. 由于您未指定PL和颜色表示方式,因此很难建议如何有效地做到这一点。

Why do we need extra space, as given in question? 为什么我们需要额外的空间? Because Insertion sort is in-place algorithm 因为插入排序是就地算法

Even insertion sort uses some extra space for local variables. 甚至插入排序也会为局部变量使用一些额外的空间。

PS I do not write ready-to-use code as it's your execise. PS我不写现成的代码,因为它是您的执行力。 Feel free to update answer with your code if something goes wrong when you implement it in code. 如果在代码中实现错误,请随时使用代码更新答案。

I don't think you should (or need to) assume that closer buckets have higher chance of having the same color. 我不认为您应该(或需要)假设较近的水桶具有相同颜色的可能性更高。 The problem can be solved maintaining indices of the first occurrence of two out of the three colors (the ones not considered "first" in the ordering). 可以解决该问题,并保持三种颜色中两种颜色的第一种出现的索引(在顺序中不被认为是“第一种”的那些)。 Replacing colors with values 0, 1, 2, I believe this code satisfies the requirements: 我用0、1、2替换颜色,我相信这段代码可以满足要求:

public class Z {
  private static int first1, first2;

  private static void swap(int[] v, int i, int j) {
    int tmp = v[i];
    v[i] = v[j];
    v[j] = tmp;
  }

  private static void sort(int[] v) {
    first1 = -1;
    first2 = -1;
    for(int i=0; i<v.length; i++) {
      switch(v[i]) {
        case 0:
          if (first1 != -1) {
            swap(v, i, first1);
            if (first2 != -1) {
              swap(v, i, first2);
              first2++;
            }
            first1++;
          } else if (first2 != -1) {
            swap(v, i, first2);
            first2++;
          }
          break;
        case 1:
          if (first2 != -1) {
            swap(v, i, first2);
            if (first1 == -1) {
              first1 = first2;
            }
            first2++;
          } else if (first1 == -1) {
            first1 = i;
          }
          break;
        case 2:
          if (first2 == -1) {
            first2 = i;
          }
          break;
      }
    }
  }
  public static String toString(int[] v) {
    StringBuilder sb = new StringBuilder();
    sb.append("[");
    for(int i=0; i<v.length; i++) {
      sb.append(v[i]);
      sb.append(" ");
    }
    sb.append("]");
    return sb.toString();
  }

  public static void main(String[] args) {
    int c = Integer.parseInt(args[0]);
    int[] v = new int[c];
    while(true) {
      int[] tmp = v.clone();
      System.out.print(toString(tmp) + "=>");
      sort(tmp);
      System.out.println(toString(tmp));
      int prev = -1;
      for(int j=0; j<tmp.length; j++) {
        if (tmp[j] < prev) {
          throw new Error();
        }
      }
      int j;
      for(j=0; j<c; j++) {
        v[j]++;
        if (v[j] == 3) {
          v[j] = 0;
        } else {
          break;
        }
      }
      if (j == c) {
        break;
      }
   }
  }
}

As a hint, Wikipedia says the following, emphasis mine: 作为提示, Wikipedia强调以下内容:

The Dutch national flag problem is a computer science programming problem proposed by Edsger Dijkstra. 荷兰国旗问题是由Edsger Dijkstra提出的计算机科学编程问题。 The flag of the Netherlands consists of three colors: red, white and blue. 荷兰的国旗由三种颜色组成:红色,白色和蓝色。 Given balls of these three colors arranged randomly in a line (the actual number of balls does not matter), the task is to arrange them such that all balls of the same color are together and their collective color groups are in the correct order. 给定这三种颜色的球随机排列在一条直线上(球的实际数量无关紧要),任务是将它们排列成所有相同颜色的球都在一起并且它们的集体颜色组正确的顺序。

The solution to this problem is of interest for designing sorting algorithms; 解决此问题的方法对于设计排序算法很有意义。 in particular, variants of the quicksort algorithm that must be robust to repeated elements need a three-way partitioning function that groups items less than a given key (red), equal to the key (white) and greater than the key (blue). 特别地, 必须对重复元素具有鲁棒性的快速排序算法的变体需要三向分区功能,该功能可以对小于给定键(红色),等于键(白色)且大于键(蓝色)的项目进行分组。 Several solutions exist that have varying performance characteristics, tailored to sorting arrays with either small or large numbers of repeated elements. 存在几种具有变化的性能特征的解决方案,这些解决方案适合于对具有少量或大量重复元素的数组进行排序。

(For the solution, the article also has pseudocode for the algorithm) (对于解决方案, 本文还提供了该算法的伪代码

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

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