License Attribution Assurance Licenses
Lines 12
Keywords
BogoSort (3) Matlab (1)
Implements this Algorithm
Permissions
Owner: Jess Kerlin
Viewable by Everyone
Editable by All Siafoo Users
Hide
Need a quick chart or graph for your blog? Try our reStructured Text renderer. Join Siafoo Now or Learn More

BogoSort in Matlab Atom Feed 0

In Brief Only as ridiculously inefficient as it needs to be.
# 's
 1function sorted = bogoSort(x)
2done = 0;
3if all(x(2:end)-x(1:end-1) >= 0)
4 done = 1;
5 sorted = x;
6end
7while done == 0
8 sorted = x(randperm(length(x)));
9 if all(sorted(2:end)-sorted(1:end-1) >= 0)
10 done = 1;
11 end
12end

Only as ridiculously inefficient as it needs to be.

Comments

over 8 years ago (05 Aug 2008 at 05:49 AM) by Theodore Test
Fun! One minor correction, though -- unless the first sort-check comes before the first shuffle, a pre-sorted array won't be returned properly.

Not like anyone really cares if bogosort gets any slower. :)
over 8 years ago (06 Aug 2008 at 09:24 PM) by Stou S.
I don't know the sorted comparison is at least clever
over 8 years ago (07 Aug 2008 at 07:50 AM) by Theodore Test
Oh, definitely. My comment was just a niggling little technicality.
over 8 years ago (11 Aug 2008 at 12:25 AM) by Jess Kerlin
Corrected to cover this contingency.