Skip to content

Commit 0b33e67

Browse files
jasmaajiegillet
authored andcommitted
Implement bogo in ruby (#360)
* Implement bogo in ruby
1 parent a335dd3 commit 0b33e67

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

contents/bogo_sort/bogo_sort.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ In code, it looks something like this:
4141
[import:11-16, lang:"php"](code/php/bogo_sort.php)
4242
{% sample lang="nim" %}
4343
[import:16-18, lang:"nim"](code/nim/bogo_sort.nim)
44+
{% sample lang="ruby" %}
45+
[import:12-16, lang:"ruby"](code/ruby/bogo.rb)
4446
{% endmethod %}
4547

4648
That's it.
@@ -81,6 +83,8 @@ We are done here!
8183
[import, lang:"php"](code/php/bogo_sort.php)
8284
{% sample lang="nim" %}
8385
[import, lang:"nim"](code/nim/bogo_sort.nim)
86+
{% sample lang="ruby" %}
87+
[import, lang:"ruby"](code/ruby/bogo.rb)
8488
{% endmethod %}
8589

8690

contents/bogo_sort/code/ruby/bogo.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env ruby
2+
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
10+
end
11+
12+
def bogo_sort(a)
13+
while !is_sorted(a)
14+
a.shuffle!
15+
end
16+
end
17+
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\nSorted")
27+
print(a)
28+
end
29+
30+
main()
31+

0 commit comments

Comments
 (0)