In [1]:
%matplotlib inline

import matplotlib
import numpy as np
import matplotlib.pyplot as plt

In [2]:
import random

def naive(input):
    input = list(input)
    n = len(input)
    for i in range(n):
        j = random.randint(0,n-1)
        input[i], input[j] = input[j], input[i]

    return "".join(input)

In [3]:
c = {
    '123': 0,
    '132': 0,
    '213': 0,
    '231': 0,
    '312': 0,
    '321': 0
}

for i in range(10000):
    c[naive('123')] += 1
    
print(c)


{'321': 1462, '213': 1849, '132': 1837, '123': 1534, '231': 1814, '312': 1504}

In [4]:
labels = list(sorted(c.keys()))
values = [c[x] for x in labels]
y_pos = np.arange(len(values))

plt.bar(y_pos, values, align='center', alpha=0.5, color='blue')
plt.xticks(y_pos, labels)
plt.title("Naive Sort")


Out[4]:
<matplotlib.text.Text at 0x10a5ca128>

In [5]:
def fisheryates(input):
    input = list(input)
    l = len(input)
    for i in range(l):
        j = random.randint(i,l-1)
        input[i], input[j] = input[j], input[i]

    return "".join(input)

In [6]:
c = {
    '123': 0,
    '132': 0,
    '213': 0,
    '231': 0,
    '312': 0,
    '321': 0
}

for i in range(10000):
    c[fisheryates('123')] += 1
    
print(c)


{'321': 1625, '213': 1735, '132': 1692, '123': 1640, '231': 1636, '312': 1672}

In [7]:
labels = list(sorted(c.keys()))
values = [c[x] for x in labels]
y_pos = np.arange(len(values))

plt.bar(y_pos, values, align='center', alpha=0.5, color='blue')
plt.xticks(y_pos, labels)
plt.title("Fisher-Yates Sort")


Out[7]:
<matplotlib.text.Text at 0x10a7066d8>

In [ ]: