From 250bc34af6af6924f5ff3653ecb501aa81bf98ef Mon Sep 17 00:00:00 2001 From: Dosenji <61090023@kmitl.ac.th> Date: Sat, 19 Oct 2019 12:15:43 +0700 Subject: [PATCH] Create bogo-sort.py simple bogo sort written in python --- Algorithms/sorting/bogo-sort.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Algorithms/sorting/bogo-sort.py diff --git a/Algorithms/sorting/bogo-sort.py b/Algorithms/sorting/bogo-sort.py new file mode 100644 index 0000000..84778b2 --- /dev/null +++ b/Algorithms/sorting/bogo-sort.py @@ -0,0 +1,24 @@ +''' +###pseudocode### +while not Sorted(list) do + shuffle (list) +done +''' +import random +def bogoSort(lst): + while(is_sorted(lst) != True): + shuffle(lst) + +def shuffle(lst): + for i in range(0, len(lst)): + r = random.randint(0, len(lst) - 1) + #shuffle + lst[i], lst[r] = lst[r], lst[i] + +#check if array is sorted or not +def is_sorted(lst): + for i in range(0, len(lst)): + if(lst[i] > lst[i + 1]): + return False + return True +