Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ You can run and edit the algorithms or contribute to them using [Gitpod.io](http
* other
* [tic_tac_toe](https://github.com/TheAlgorithms/MATLAB-Octave/blob/master/algorithms/other/tic_tac_toe.m)
* sorting
* [Bogo Sort](https://github.com/TheAlgorithms/MATLAB-Octave/blob/master/algorithms/sorting/bogo_sort.m)
* [Bubble Sort](https://github.com/TheAlgorithms/MATLAB-Octave/blob/master/algorithms/sorting/bubble_sort.m)
* [Counting Sort](https://github.com/TheAlgorithms/MATLAB-Octave/blob/master/algorithms/sorting/counting_sort.m)
* [Insertion Sort](https://github.com/TheAlgorithms/MATLAB-Octave/blob/master/algorithms/sorting/insertion_sort.m)
Expand Down
25 changes: 25 additions & 0 deletions algorithms/sorting/bogo_sort.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
% This is a pure Matlab implementation of the bogosort algorithm,
% also known as permutation sort, stupid sort, slowsort, shotgun sort, or monkey sort.
% Bogosort generates random permutations until it guesses the correct one.
% More info on: https://en.wikipedia.org/wiki/Bogosort
%
% Example-Run: bogo_sort([2,5,-10,22,48,2])


function collection = bogo_sort(collection)
while is_sorted(collection) == 0
collection = collection(randperm(length(collection)));
end
end
function is_sorted_bool = is_sorted(collection)
is_sorted_bool = 1;
if length(collection) < 2
return
end
for i = 1: (length(collection)-1)
if collection(i) > collection(i + 1)
is_sorted_bool = 0;
return
end
end
end