Gnomesort

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


[79 96 30 54 23 27 84 43  6 71 69 83 73 44 27]

So lets sort these gnomes.


In [3]:
gnomesort(seq)
print seq


[ 6 23 27 27 30 43 44 54 69 71 73 79 83 84 96]

Perfect that works! But there are different ways of doing this too...


In [ ]: