-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
31 lines (27 loc) · 822 Bytes
/
test.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
# Definition for a pair.
# class Pair:
# def __init__(self, key: int, value: str):
# self.key = key
# self.value = value
# Implementation of Insertion Sort
class Pair:
def __init__(self, key: int, value: str):
self.key = key
self.value = value
def __str__(self):
return f'({self.key}, {self.value})'
def __repr__(self):
return str(self)
def insertionSort(pairs):
n = len(pairs)
res = [] # To store the intermediate states of the array
for i in range(n):
j = i - 1
# Move elements that are greater than key one position ahead
while j >= 0 and pairs[j].key > pairs[j + 1].key:
pairs[j], pairs[j + 1] = pairs[j + 1], pairs[j]
j -= 1
# Clone and save the entire state of the array at this point
res.append(pairs[:])
return res
print(insertionSort([Pair(3, 'three')]))