File tree Expand file tree Collapse file tree 2 files changed +10
-22
lines changed Expand file tree Collapse file tree 2 files changed +10
-22
lines changed Original file line number Diff line number Diff line change @@ -44,7 +44,7 @@ In code, it looks something like this:
44
44
{% sample lang="nim" %}
45
45
[ import:16-18, lang:"nim"] ( code/nim/bogo_sort.nim )
46
46
{% sample lang="ruby" %}
47
- [ import:12-16 , lang:"ruby"] ( code/ruby/bogo.rb )
47
+ [ import:7-9 , lang:"ruby"] ( code/ruby/bogo.rb )
48
48
{% endmethod %}
49
49
50
50
That's it.
Original file line number Diff line number Diff line change 1
1
#!/usr/bin/env ruby
2
2
3
3
def is_sorted ( a )
4
- for i in 0 ...a . length -1
5
- if a [ i +1 ] < a [ i ]
6
- return false
7
- end
8
- end
9
- return true
4
+ a . each_cons ( 2 ) . all? { |( l , r ) | l <= r }
10
5
end
11
6
12
7
def bogo_sort ( a )
13
- while !is_sorted ( a )
14
- a . shuffle!
15
- end
8
+ a . shuffle! until is_sorted a
16
9
end
17
10
18
- def main ( )
19
- a = [ 1 , 1 , 0 , 3 , 7 ]
20
-
21
- puts ( "Unsorted" )
22
- print ( a )
23
-
24
- bogo_sort ( a )
25
-
26
- puts ( "\n \n Sorted" )
27
- print ( a )
28
- end
11
+ a = [ 1 , 1 , 0 , 3 , 7 ]
12
+
13
+ puts "Unsorted"
14
+ p a
29
15
30
- main ( )
16
+ bogo_sort a
31
17
18
+ puts "Sorted"
19
+ p a
You can’t perform that action at this time.
0 commit comments