License New BSD license
Lines 18
Keywords
bogosort (5) numpy (3) python (33)
Implements this Algorithm
Permissions
Owner: Theodore Test
Viewable by Everyone
Editable by All Siafoo Users
Hide
Stay up to dateembedded code automagically updates, each snippet and article has a feed Join Siafoo Now or Learn More

Bogosort Implementation with Python Numpy Atom Feed 0

In Brief A NumPy-based implementation of the ever-hilarious Bogosort algorithm. Tries (hopelessly) to optimize the algorithm by taking advantage of lazy iterators, NumPy C-based shuffling, and minimizing reference call overhead.
# 's
 1from numpy.random import shuffle as n_shuffle
2from numpy import array
3def bogosort(my_array):
4
5 # Let's make a local-scope-only array of the proper type.
6 # This may add a little unfair overhead to comparisons,
7 # but the value of type-normalization is generally self-evident.
8 a = array(my_array)
9
10
11 s = a.size -1
12
13 if s <= 0:
14 return a
15
16 itr = xrange(s)
17
18 while True:
19 for x in itr:
20 if a[x] > a[x+1]:
21 break
22 else:
23 return a
24
25 n_shuffle(a)

A NumPy-based implementation of the ever-hilarious Bogosort algorithm. Tries (hopelessly) to optimize the algorithm by taking advantage of lazy iterators, NumPy C-based shuffling, and minimizing reference call overhead.