Skip to content

Commit 4bb9d0b

Browse files
depateleios
authored andcommitted
Bogosort fortran (#421)
* Delete monte_carlo_pi.f90 * init * further implementation * next * hopefully fixes things * initial commit * final implementation and documentation * capitalising the syntax
1 parent 1be7b21 commit 4bb9d0b

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

contents/bogo_sort/bogo_sort.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ In code, it looks something like this:
4444
{% sample lang="nim" %}
4545
[import:16-18, lang:"nim"](code/nim/bogo_sort.nim)
4646
{% sample lang="ruby" %}
47-
[import:7-9, lang:"ruby"](code/ruby/bogo.rb)
47+
[import:12-16, lang:"ruby"](code/ruby/bogo.rb)
48+
{% sample lang="f90" %}
49+
[import:24-32, lang:"fortran"](code/fortran/bogo.f90)
4850
{% endmethod %}
4951

5052
That's it.
5153
Ship it!
5254
We are done here!
5355

54-
## Example Code
55-
56+
## Example Code
5657
{% method %}
5758
{% sample lang="jl" %}
5859
[import, lang:"julia"](code/julia/bogo.jl)
@@ -89,6 +90,8 @@ We are done here!
8990
[import, lang:"nim"](code/nim/bogo_sort.nim)
9091
{% sample lang="ruby" %}
9192
[import, lang:"ruby"](code/ruby/bogo.rb)
93+
{% sample lang="f90" %}
94+
[import, lang:"fortran"](code/fortran/bogo.f90)
9295
{% endmethod %}
9396

9497

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
PROGRAM bogo
2+
IMPLICIT NONE
3+
REAL(8), DIMENSION(5) :: array
4+
5+
array = (/ 1d0, 1d0, 0d0, 3d0, 7d0 /)
6+
7+
CALL bogo_sort(array)
8+
9+
WRITE(*,*) array
10+
11+
contaINs
12+
13+
LOGICAL FUNCTION is_sorted(array)
14+
REAL(8), DIMENSION(:), INTENT(IN) :: array
15+
INTEGER :: i
16+
17+
DO i = 1, SIZE(array)
18+
IF (array(i+1) < array(i)) THEN
19+
is_sorted = .FALSE.
20+
END IF
21+
END DO
22+
END FUNCTION is_sorted
23+
24+
SUBROUTINE bogo_sort(array)
25+
REAL(8), DIMENSION(:), INTENT(INOUT) :: array
26+
27+
DO WHILE (is_sorted(array) .EQV. .FALSE.)
28+
29+
CALL shuffle(array)
30+
31+
END DO
32+
END SUBROUTINE bogo_sort
33+
34+
SUBROUTINE shuffle(array)
35+
REAL(8), DIMENSION(:), INTENT(INOUT) :: array
36+
INTEGER :: i, randpos
37+
REAL(8) :: r, temp
38+
39+
DO i = size(array), 2, -1
40+
CALL RANDOM_NUMBER(r)
41+
randpos = INT(r * i) + 1
42+
temp = array(randpos)
43+
array(randpos) = array(i)
44+
array(i) = temp
45+
END DO
46+
47+
END SUBROUTINE shuffle
48+
END PROGRAM bogo

0 commit comments

Comments
 (0)