-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelection_Algorithm.py
95 lines (85 loc) · 2.59 KB
/
Selection_Algorithm.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
from Linkedlist import Linkedlist
from random import randrange
Linked_list = Linkedlist()
def Selection_Sort_Array(arr):
for i in range(0, len(arr) - 1):
mn = arr[0]
pos = 0
for j in range(0, len(arr) - i ):
if (arr[j] > mn):
mn = arr[j]
pos = j
arr[pos], arr[len(arr) - i - 1] = arr[len(arr) - i - 1], arr[pos]
return arr
def selectionsort_linked(list):
I= list.head
while I != None:
J = I.getNext()
min = I
while J != None:
if J.getData() > min.getData():
min = J
J = J.getNext()
TEMP = min.getData()
min.setData(I.getData())
I.setData(TEMP)
I = I.getNext()
def Selection_Sort_Linked_List(linked_list):
flag = True
cur = linked_list.head
precur = None
while flag:
min_node = cur
min_prev = None
nxt = min_node.getNext()
prev = cur
flag = False
while nxt != None:
if nxt.getData() < min_node.getData():
min_prev = prev
min_node = nxt
flag = True
if nxt.getNext() != None and nxt.getData() > nxt.getNext().getData():
flag = True
prev = nxt
nxt = nxt.getNext()
cur_nxt = cur.getNext()
min_nxt = min_node.getNext()
if (precur == None) and cur.getNext() == min_node:
linked_list.head = min_node
min_node.setNext(cur)
cur.setNext(min_nxt)
precur = linked_list.head
cur = min_node
if (precur == None) and cur != min_node:
linked_list.head = min_node
linked_list.head.setNext(cur_nxt)
min_prev.setNext(cur)
cur.setNext(min_nxt)
precur = linked_list.head
cur = min_node
elif cur.getNext() == min_node:
precur.setNext(min_node)
min_node.setNext(cur)
cur.setNext(min_nxt)
cur = min_node
elif cur != min_node:
precur.setNext(min_node)
min_node.setNext(cur_nxt)
min_prev.setNext(cur)
cur.setNext(min_nxt)
cur = min_node
precur = cur
cur = cur.getNext()
"""Linked_list = Linkedlist()
Linked_list.add(2)
Linked_list.add(4)
Linked_list.add(1)
Linked_list.add(5)
Linked_list.add(3)
Selection_Sort_Linked_List(Linked_list)
current = Linked_list.head
while (current != None):
print(current.getData())
current = current.getNext()"""
"""Selection_Sort_Linked_List(Linked_list)"""