-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathMergeSort.py
122 lines (91 loc) · 2.62 KB
/
MergeSort.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Python program to perform Merge Sort on a Signly linked list
# Node class
class Node:
# Constructor to initialise data and next
def __init__(self, data=None):
self.data = data
self.next = None
class SinglyLinkedList:
# Constructor to initialise head
def __init__(self, head=None):
self.head = head
# Function to Insert data at the beginning of the linked list
def insert_at_beg(self, data):
node = Node(data)
node.next = self.head
self.head = node
# Function to print the linked list
def print_data(self):
current = self.head
while current is not None:
print(current.data, '-> ', end='')
current = current.next
print('None')
# Function to split the linked list into two halves
def split(head):
slow = head
if slow is None or slow.next is None:
return head, None
fast = slow.next
# Reach to the middle of the linked list
while fast is not None:
fast = fast.next
if fast is not None:
fast = fast.next
slow = slow.next
fast = slow.next
# break the linked list in half
slow.next = None
# return the 2 linked lists formed
return head, fast
# Function to merge linked lists in sorted order
def merge(a, b):
# Make a dummy node
dummy = Node()
# dummy node next will be the head of our merged list
dummy.next = None
temp = SinglyLinkedList(dummy)
tail = temp.head
while True:
if a is None:
tail.next = b
break
elif b is None:
tail.next = a
break
elif a.data <= b.data:
tail.next = a
a = a.next
else:
tail.next = b
b = b.next
tail = tail.next
return temp.head.next
# Function Merge Sort
def merge_sort(head):
if head is None or head.next is None:
return head
a, b = split(head)
a = merge_sort(a)
b = merge_sort(b)
head = merge(a, b)
return head
if __name__ == '__main__':
linked_list = SinglyLinkedList()
linked_list.insert_at_beg(9)
linked_list.insert_at_beg(3)
linked_list.insert_at_beg(2)
linked_list.insert_at_beg(1)
linked_list.insert_at_beg(5)
linked_list.insert_at_beg(4)
linked_list.insert_at_beg(8)
linked_list.insert_at_beg(7)
linked_list.insert_at_beg(6)
# before sorting
print('before sorting')
linked_list.print_data()
# call merge_sort function
linked_list.head = merge_sort(linked_list.head)
# after sorting
print('after sorting')
linked_list.print_data()