Difference between revisions of "TADM2E 4.4"

From Algorithm Wiki
Jump to: navigation, search
(Recovering wiki)
 
m (add stable sort)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
Algorithm:
+
Any stable sort can solve this problem, using the color as the key:
 +
such as insertion sort, merge sort or bucket sort(below)
  
<pre>
+
Bucket Sort Algorithm:
 +
 
 +
<pre>
 
Create 3 buckets, one each for red, blue and yellow.
 
Create 3 buckets, one each for red, blue and yellow.
 
for each Pair P in the input
 
for each Pair P in the input
Line 7: Line 10:
  
 
Output the data from the red, blue and yellow buckets.
 
Output the data from the red, blue and yellow buckets.
&lt;/pre&gt;
+
</pre>

Latest revision as of 00:54, 8 July 2016

Any stable sort can solve this problem, using the color as the key: such as insertion sort, merge sort or bucket sort(below)

Bucket Sort Algorithm:

Create 3 buckets, one each for red, blue and yellow.
for each Pair P in the input
   append P.number to the bucket P.color

Output the data from the red, blue and yellow buckets.