Skip to content

Commit 3e14809

Browse files
9967hangoswami-rahul
authored andcommitted
Created bogo_sort.py in algorithms/sort (keon#308)
* Create bogo_sort.py * Update README.md * Update README_CN.md * Update README_GE.md * Update README_JP.md * Update README_KR.md * Update test_sort.py * Update __init__.py * Update test_sort.py * Update test_sort.py * Update bogo_sort.py * Update bogo_sort.py
1 parent 59db106 commit 3e14809

File tree

8 files changed

+33
-0
lines changed

8 files changed

+33
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ If you want to uninstall algorithms, it is as simple as:
220220
- [randomized_set](algorithms/set/randomized_set.py)
221221
- [set_covering](algorithms/set/set_covering.py)
222222
- [sort](algorithms/sort)
223+
- [bogo_sort](algorithms/sort/bogo_sort.py)
223224
- [bubble_sort](algorithms/sort/bubble_sort.py)
224225
- [bucket_sort](algorithms/sort/bucket_sort.py)
225226
- [comb_sort](algorithms/sort/comb_sort.py)

README_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ pip3 uninstall -y algorithms
202202
- [randomized_set:随机集合](algorithms/set/randomized_set.py)
203203
- [set_covering:集合覆盖](algorithms/set/set_covering.py)
204204
- [sort:排序](algorithms/sort)
205+
- [bogo_sort](algorithms/sort/bogo_sort.py)
205206
- [bubble_sort:冒泡排序](algorithms/sort/bubble_sort.py)
206207
- [bucket_sort](algorithms/sort/bucket_sort.py)
207208
- [comb_sort:梳排序](algorithms/sort/comb_sort.py)

README_GE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
227227
- [randomized_set](algorithms/set/randomized_set.py)
228228
- [set_covering](algorithms/set/set_covering.py)
229229
- [sort](algorithms/sort)
230+
- [bogo_sort](algorithms/sort/bogo_sort.py)
230231
- [bubble_sort](algorithms/sort/bubble_sort.py)
231232
- [bucket_sort](algorithms/sort/bucket_sort.py)
232233
- [comb_sort](algorithms/sort/comb_sort.py)

README_JP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ if __name__ == "__main__":
217217
- [randomized_set](algorithms/set/randomized_set.py)
218218
- [set_covering](algorithms/set/set_covering.py)
219219
- [sort : ソート](algorithms/sort)
220+
- [bogo_sort](algorithms/sort/bogo_sort.py)
220221
- [bubble_sort](algorithms/sort/bubble_sort.py)
221222
- [bucket_sort](algorithms/sort/bucket_sort.py)
222223
- [comb_sort](algorithms/sort/comb_sort.py)

README_KR.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ if __name__ == "__main__":
213213
- [randomized_set](algorithms/set/randomized_set.py)
214214
- [set_covering](algorithms/set/set_covering.py)
215215
- [sort : 정렬 알고리즘](algorithms/sort)
216+
- [bogo_sort](algorithms/sort/bogo_sort.py)
216217
- [bubble_sort](algorithms/sort/bubble_sort.py)
217218
- [comb_sort](algorithms/sort/comb_sort.py)
218219
- [counting_sort](algorithms/sort/counting_sort.py)

algorithms/sort/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .bogo_sort import *
12
from .bubble_sort import *
23
from .comb_sort import *
34
from .counting_sort import *

algorithms/sort/bogo_sort.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import random
2+
3+
def bogo_sort(arr):
4+
"""Bogo Sort
5+
Best Case Complexity: O(n)
6+
Worst Case Complexity: O(∞)
7+
Average Case Complexity: O(n(n-1)!)
8+
"""
9+
def is_sorted(arr):
10+
#check the array is inorder
11+
i = 0
12+
arr_len = len(arr)
13+
while i+1 < arr_len:
14+
if arr[i] > arr[i+1]:
15+
return False
16+
i += 1
17+
return True
18+
while not is_sorted(arr):
19+
random.shuffle(arr)
20+
return arr
21+
22+

tests/test_sort.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from algorithms.sort import (
2+
bogo_sort,
23
bubble_sort,
34
comb_sort,
45
counting_sort,
@@ -16,6 +17,10 @@
1617

1718

1819
class TestSuite(unittest.TestCase):
20+
def test_bogo_sort(self):
21+
self.assertEqual([1, 5, 23],
22+
bogo_sort([1, 23, 5]))
23+
1924
def test_bubble_sort(self):
2025
self.assertEqual([1, 5, 23, 57, 65, 1232],
2126
bubble_sort([1, 5, 65, 23, 57, 1232]))

0 commit comments

Comments
 (0)