-
Notifications
You must be signed in to change notification settings - Fork 0
/
cviceni6.py
69 lines (61 loc) · 1.68 KB
/
cviceni6.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
class SortedList:
def __init__(self):
self.head = None
def member(self, value):
it = self.head
while it != None and it.value < value:
it = it.next
return it != None and it.value == value
def insert(self, value):
prev = None
it = self.head
while it != None and it.value < value:
prev = it
it = it.next
if it != None and it.value == value:
return
if prev == None:
self.head = Node(value, it)
else:
prev.next = Node(value, it)
def remove(self, value):
prev = None
it = self.head
while it != None and it.value < value:
prev = it
it = it.next
if it == None or it.value != value:
return
if prev == None:
self.head = it.next
else:
prev.next = it.next
def print(self):
it = self.head
while it != None:
print(it.value, end=' ')
it = it.next
print()
def merge(left, right):
l = 0; r = 0; merged = []
while True:
hasLeft = l < len(left)
hasRight = r < len(right)
if hasLeft and (not hasRight or left[l] < right[r]):
merged.append(left[l])
l += 1
elif hasRight:
merged.append(right[r])
r += 1
else:
break
return merged
def mergesort(array):
if len(array) <= 1:
return array
mid = len(array) // 2
return merge(mergesort(array[:mid]), mergesort(array[mid:]))