diff --git a/contents/bogo_sort/bogo_sort.md b/contents/bogo_sort/bogo_sort.md index 90970542c..26083527e 100644 --- a/contents/bogo_sort/bogo_sort.md +++ b/contents/bogo_sort/bogo_sort.md @@ -44,15 +44,16 @@ In code, it looks something like this: {% sample lang="nim" %} [import:16-18, lang:"nim"](code/nim/bogo_sort.nim) {% sample lang="ruby" %} -[import:7-9, lang:"ruby"](code/ruby/bogo.rb) +[import:12-16, lang:"ruby"](code/ruby/bogo.rb) +{% sample lang="f90" %} +[import:24-32, lang:"fortran"](code/fortran/bogo.f90) {% endmethod %} That's it. Ship it! We are done here! -## Example Code - +## Example Code {% method %} {% sample lang="jl" %} [import, lang:"julia"](code/julia/bogo.jl) @@ -89,6 +90,8 @@ We are done here! [import, lang:"nim"](code/nim/bogo_sort.nim) {% sample lang="ruby" %} [import, lang:"ruby"](code/ruby/bogo.rb) +{% sample lang="f90" %} +[import, lang:"fortran"](code/fortran/bogo.f90) {% endmethod %} diff --git a/contents/bogo_sort/code/fortran/bogo.f90 b/contents/bogo_sort/code/fortran/bogo.f90 new file mode 100644 index 000000000..87a7ab9ff --- /dev/null +++ b/contents/bogo_sort/code/fortran/bogo.f90 @@ -0,0 +1,48 @@ +PROGRAM bogo + IMPLICIT NONE + REAL(8), DIMENSION(5) :: array + + array = (/ 1d0, 1d0, 0d0, 3d0, 7d0 /) + + CALL bogo_sort(array) + + WRITE(*,*) array + +contaINs + + LOGICAL FUNCTION is_sorted(array) + REAL(8), DIMENSION(:), INTENT(IN) :: array + INTEGER :: i + + DO i = 1, SIZE(array) + IF (array(i+1) < array(i)) THEN + is_sorted = .FALSE. + END IF + END DO + END FUNCTION is_sorted + + SUBROUTINE bogo_sort(array) + REAL(8), DIMENSION(:), INTENT(INOUT) :: array + + DO WHILE (is_sorted(array) .EQV. .FALSE.) + + CALL shuffle(array) + + END DO + END SUBROUTINE bogo_sort + + SUBROUTINE shuffle(array) + REAL(8), DIMENSION(:), INTENT(INOUT) :: array + INTEGER :: i, randpos + REAL(8) :: r, temp + + DO i = size(array), 2, -1 + CALL RANDOM_NUMBER(r) + randpos = INT(r * i) + 1 + temp = array(randpos) + array(randpos) = array(i) + array(i) = temp + END DO + + END SUBROUTINE shuffle +END PROGRAM bogo