The most basic so0rtinng algorithm for a sequence of gnomes.
In [1]:
def gnomesort(seq):
i = 0
while i < len(seq):
if i == 0 or seq[i-1] <= seq[i]:
i += 1
else:
seq[i], seq[i-1] = seq[i-1],seq[i]
i -= 1
In [2]:
import numpy as np
seq = np.random.randint(0,100,15)
print seq
So lets sort these gnomes.
In [3]:
gnomesort(seq)
print seq
Perfect that works! But there are different ways of doing this too...
In [ ]: