-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertion_sort_linked_lists.py
59 lines (47 loc) · 1.28 KB
/
insertion_sort_linked_lists.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
class Node():
def __init__(self):
self.val = None
self.next = None
def print_list(L):
while L != None:
print(L.val, end=" -> ")
L = L.next
print("\n")
def add_el_to_list_front(A,el):
x = Node()
x.val = el
x.next = A
return x
def add_node_to_list_front(A,x):
x.next = A
return x
def tab_to_list(tab):
n = len(tab)
A = None
for i in range(n-1,-1,-1):
x = Node()
x.val = tab[i]
A = add_node_to_list_front(A,x)
return A
def insertion_sort_LL(L): #niedokonczone
if L == None: return
i = L #wskaznik na node przed tym ktory rozważamy
while i.next != None:
if i.next.val < L.val:
s = i.next.next
i.next.next = L
L = i.next
i.next = s
continue
j = L #wskaznik na element po którym mamy wstawic i.next
while j.next != i.next and j.next.val<=i.next.val: j = j.next
if j.next == i.next: #jak i jest na swoim miejscu
i = i.next
continue
big_fella = j.next
si = i.next.next
sj = j.next.next
j.next = i.next
j.next.next = sj
i.next = big_fella
i.next.next = si