Dutch national flag problem: Difference between revisions

m
(FORTRAN entry)
Line 469:
=={{header|FORTRAN}}==
Please find the example run along with compilation instructions on a GNU/linux platform in the comments at the beginning of the FORTRAN 2008 program source. The Netherlands program, using equal numbers of colors, solved the problem at three sample sizes. Swaps number 2/3 the total of samples, convincingly demonstrating the O(n) time behavior that's directly provable by inspection. The color strings are chosen for ASCII sort. Feature not used.
 
Abhor code duplication. I've repeated code anyway to demonstrate FORTRAN pointers, which behave like an alias. A subroutine with traditional arguments including the number of valid elements of the array is appropriate. I'd use one long array instead of 3 arrays and the size intrinsic.
<lang>
!-*- mode: compilation; default-directory: "/tmp/" -*-
!Compilation started at Mon Jun 3 0011:0418:4924
!
!a=./f && make FFLAGS='-O0 -g' $a && OMP_NUM_THREADS=2 $a < unixdict.txt
Line 482 ⟶ 484:
! 9999 items, 6666 swaps.
!
!Compilation finished at Mon Jun 3 0011:0418:4924
 
program Netherlands
Line 488 ⟶ 490:
character(len=6), parameter, dimension(3) :: colors = (/'RED ', 'WHITE ', 'blue '/)
integer, dimension(12) :: sort_me
integer, dimension(999), target :: a999
integer, dimension(9999), target :: a9999
integer, dimension(:), pointer :: pi
integer :: i, swaps
data sort_me/4*1,4*2,4*3/
data a999/333*1,333*2,333*3/
data a9999/3333*1,3333*2,3333*3/
call shuffle(sort_me, 5)
write(6,*)'Original and flag sequences'
Line 500 ⟶ 501:
write(6,*) (colors(sort_me(i)), i = 1, size(sort_me))
write(6,*) 12,'items,',swaps,' swaps.'
pi => a999
call shuffle(a999, 333+1)
do i=1, size(pi)
call partition3way(a999, 2, swaps)
pi(i) = 1 + L(size(pi)/3 .lt. i) + L(2*size(pi)/3 .lt. i)
write(6,*) 999,'items,',swaps,' swaps.'
end do
call shuffle(a9999, 3333+1)
call partition3wayshuffle(a9999pi, 2, swapssize(pi)/3+1)
writecall partition3way(6pi,*) 99992,'items,',swaps,' swaps.')
write(6,*) 999size(pi),'items,',swaps,' swaps.'
pi => a9999
do i=1, size(pi)
pi(i) = 1 + L(size(pi)/3 .lt. i) + L(2*size(pi)/3 .lt. i)
end do
call shuffle(a999pi, 333size(pi)/3+1)
call partition3way(a999pi, 2, swaps)
write(6,*) size(pi),'items,',swaps,' swaps.'
 
contains
Line 569 ⟶ 578:
end program Netherlands
</lang>
 
=={{header|Go}}==
<lang go>package main
Anonymous user