forked from zamhown/sorting-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonkeysort.py
38 lines (36 loc) · 1.1 KB
/
monkeysort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Script Name : monkeysort.py
# Author : Howard Zhang
# Created : 14th June 2018
# Last Modified : 14th June 2018
# Version : 1.0
# Modifications :
# Description : Monkey sorting algorithm which can do nothing but be funny.
import copy
import random
from data import Data
def monkey_sort(data_set, frame_count):
# FRAME OPERATION BEGIN
frames = [data_set]
# FRAME OPERATION END
dataes = [data.value for data in data_set]
flag = False
while not flag:
flag = True
for i in range(Data.data_count - 1):
# FRAME OPERATION BEGIN
ds = [Data(d) for d in dataes]
frames.append(ds)
ds[i].set_color('r')
ds[i+1].set_color('k')
if len(frames) == frame_count:
return frames
# FRAME OPERATION END
if dataes[i] > dataes[i+1]:
flag = False
break
if not flag:
random.shuffle(dataes)
# FRAME OPERATION BEGIN
frames.append(Data(d) for d in dataes)
return frames
# FRAME OPERATION END