Skip to content

Bogosort fortran #421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions contents/bogo_sort/bogo_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 %}


Expand Down
48 changes: 48 additions & 0 deletions contents/bogo_sort/code/fortran/bogo.f90
Original file line number Diff line number Diff line change
@@ -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