June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,4 +1,4 @@
|
|||
[[File:Dutch_flag_3.jpg|200px||right]]
|
||||
[[File:Dutch_flag_3.jpg|350px||right]]
|
||||
|
||||
The Dutch national flag is composed of three coloured bands in the order red then white and lastly blue. The problem posed by [[wp:Edsger Dijkstra|Edsger Dijkstra]] is:
|
||||
:Given a number of red, blue and white balls in random order, arrange them in the order of the colours Dutch national flag.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
BEGIN {
|
||||
weight[1] = "red"; weight[2] = "white"; weight[3] = "blue";
|
||||
# ballnr must be >= 3. Using very high numbers here may make your computer
|
||||
# run out of RAM. (10 millions balls ~= 2.5GiB RAM on x86_64)
|
||||
ballnr = 10
|
||||
|
||||
srand()
|
||||
# Generating a random pool of balls. This python-like loop is actually
|
||||
# a prettyfied one-liner
|
||||
do
|
||||
for (i = 1; i <= ballnr; i++)
|
||||
do
|
||||
balls[i] = int(3 * rand() + 1)
|
||||
# These conditions ensure the 3 first balls contains
|
||||
# a white, blue and red ball. Removing 'i < 4' would
|
||||
# hit performance a lot.
|
||||
while ( (i < 4 && i > 1 && balls[i] == balls[i - 1]) ||
|
||||
(i < 4 && i > 2 && balls[i] == balls[i - 2]) )
|
||||
while (is_dnf(balls, ballnr))
|
||||
|
||||
printf("BEFORE: ")
|
||||
print_balls(balls, ballnr, weight)
|
||||
|
||||
# Using gawk default quicksort. Using variants of PROCINFO["sorted_in"]
|
||||
# wasn't faster than a simple call to asort().
|
||||
asort(balls)
|
||||
|
||||
printf("\n\nAFTER : ")
|
||||
print_balls(balls, ballnr, weight)
|
||||
|
||||
sorting = is_dnf(balls, ballnr) ? "valid" : "invalid"
|
||||
print("\n\nSorting is " sorting ".")
|
||||
}
|
||||
|
||||
function print_balls(balls, ballnr, weight ,i) {
|
||||
for (i = 1; i <= ballnr; i++)
|
||||
printf("%-7s", weight[balls[i]])
|
||||
}
|
||||
|
||||
function is_dnf(balls, ballnr) {
|
||||
# Checking if the balls are sorted in the Dutch national flag order,
|
||||
# using a simple scan with weight comparison
|
||||
for (i = 2; i <= ballnr; i++)
|
||||
if (balls[i - 1] > balls[i])
|
||||
return 0
|
||||
return 1
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
BEFORE: blue red white red white blue red white blue white
|
||||
|
||||
AFTER : red red red white white white white blue blue blue
|
||||
|
||||
Sorting is valid.
|
||||
|
|
@ -6,41 +6,25 @@ public class DutchNationalFlag {
|
|||
RED, WHITE, BLUE
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args){
|
||||
DutchColors[] balls = new DutchColors[12];
|
||||
|
||||
DutchColors[] values = DutchColors.values();
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < balls.length; i++)
|
||||
balls[i] = values[rand.nextInt(values.length)];
|
||||
|
||||
for (int i = 0; i < balls.length; i++)
|
||||
balls[i]=values[rand.nextInt(values.length)];
|
||||
System.out.println("Before: " + Arrays.toString(balls));
|
||||
|
||||
dutchNationalFlagSort(balls);
|
||||
Arrays.sort(balls);
|
||||
System.out.println("After: " + Arrays.toString(balls));
|
||||
|
||||
System.out.println("After : " + Arrays.toString(balls));
|
||||
}
|
||||
|
||||
private static void dutchNationalFlagSort(DutchColors[] items) {
|
||||
int lo = 0, mid = 0, hi = items.length - 1;
|
||||
|
||||
while (mid <= hi)
|
||||
switch (items[mid]) {
|
||||
case RED:
|
||||
swap(items, lo++, mid++);
|
||||
break;
|
||||
case WHITE:
|
||||
mid++;
|
||||
break;
|
||||
case BLUE:
|
||||
swap(items, mid, hi--);
|
||||
break;
|
||||
boolean sorted = true;
|
||||
for (int i = 1; i < balls.length; i++ ){
|
||||
if (balls[i-1].compareTo(balls[i]) > 0){
|
||||
sorted=false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static void swap(DutchColors[] arr, int a, int b) {
|
||||
DutchColors tmp = arr[a];
|
||||
arr[a] = arr[b];
|
||||
arr[b] = tmp;
|
||||
}
|
||||
System.out.println("Correctly sorted: " + sorted);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
# Project : Dutch national flag problem
|
||||
# Date : 2017/11/23
|
||||
# Author : Gal Zsolt (~ CalmoSoft ~)
|
||||
# Email : <calmosoft@gmail.com>
|
||||
|
||||
flag = ["Red","White","Blue"]
|
||||
balls = list(10)
|
||||
|
||||
see "Random: |"
|
||||
for i = 1 to 10
|
||||
color = random(2) + 1
|
||||
balls[i] = flag[color]
|
||||
see balls[i] + " |"
|
||||
next
|
||||
see nl
|
||||
|
||||
see "Sorted: |"
|
||||
for i = 1 to 3
|
||||
color = flag[i]
|
||||
for j = 1 to 10
|
||||
if balls[j] = color
|
||||
see balls[j] + " |"
|
||||
ok
|
||||
next
|
||||
next
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
class Ball
|
||||
FLAG = {red: 1, white: 2, blue: 3}
|
||||
|
||||
def initialize
|
||||
@color = FLAG.keys.sample
|
||||
end
|
||||
|
||||
def color
|
||||
@color
|
||||
end
|
||||
|
||||
def <=>(other) # needed for sort, results in -1 for <, 0 for == and 1 for >.
|
||||
FLAG[self.color] <=> FLAG[other.color]
|
||||
end
|
||||
|
||||
def inspect
|
||||
@color
|
||||
end
|
||||
end
|
||||
|
||||
balls = []
|
||||
balls = Array.new(8){Ball.new} while balls == balls.sort
|
||||
|
||||
puts "Random: #{balls}"
|
||||
puts "Sorted: #{balls.sort}"
|
||||
Loading…
Add table
Add a link
Reference in a new issue