forked from zamhown/sorting-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertionsort.py
34 lines (32 loc) · 1 KB
/
insertionsort.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
# Script Name : insertionsort.py
# Author : Howard Zhang
# Created : 14th June 2018
# Last Modified : 14th June 2018
# Version : 1.0
# Modifications :
# Description : Insertion sorting algorithm.
import copy
from data import Data
def insertion_sort(data_set):
# FRAME OPERATION BEGIN
frames = [data_set]
# FRAME OPERATION END
ds = copy.deepcopy(data_set)
for i in range(1, Data.data_count):
# FRAME OPERATION BEGIN
frames.append(copy.deepcopy(ds))
frames[-1][i].set_color('r')
# FRAME OPERATION END
for j in range(i, 0, -1):
if ds[j].value < ds[j-1].value:
ds[j], ds[j-1] = ds[j-1], ds[j]
# FRAME OPERATION BEGIN
frames.append(copy.deepcopy(ds))
frames[-1][j-1].set_color('r')
# FRAME OPERATION END
else:
break
# FRAME OPERATION BEGIN
frames.append(ds)
return frames
# FRAME OPERATION END