-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting_algorithm.py
74 lines (62 loc) · 1.82 KB
/
sorting_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
import compare
import random
# All different sorting methods
# Not all of them work
def bubble(list, connn, inactive_clients):
for i in range(len(list) - 1, 0, -1):
for b in range(0, i):
if compare.left_is_smaller(list[b], list[b + 1], connn, inactive_clients):
pass
else:
list[b], list[b + 1] = list[b + 1], list[b]
return list
def swapping(tosort, start, conn):
for i in range(start, len(tosort) - 1, 2):
if compare.left_is_smaller(tosort[i], tosort[i + 1], conn):
tosort[i], tosort[i + 1] = tosort[i + 1], tosort[i]
listret = [tosort, False]
return listret
else:
listret = [tosort, True]
return listret
return
def oddEven(x, conn):
is_sorted = False
con = random.choice(conn)
while not is_sorted:
is_sorted = True
for i in range(0, len(x)-1, 2):
if compare.left_is_smaller(x[i+1], x[i], con[0]):
x[i], x[i+1] = x[i+1], x[i]
is_sorted = False
for i in range(1, len(x)-1, 2):
if compare.left_is_smaller(x[i+1], x[i], con[0]):
x[i], x[i+1] = x[i+1], x[i]
is_sorted = False
return x
def quick(list, conn, inactive_clients):
if len(list) <= 1:
return list
list1 = []
list2 = []
pivot = random.choice(list)
for number in list:
if compare.left_is_smaller(pivot, number, conn, inactive_clients):
list2.append(number)
else:
list1.append(number)
return quick(list1, conn, inactive_clients) + quick(list2, conn, inactive_clients)
def semiQuick(list, conn, inactive_clients):
if len(list) <= 1:
return list
if len(list) <= 3:
return bubble(list, conn, inactive_clients)
list1 = []
list2 = []
pivot = random.choice(list)
for number in list:
if compare.left_is_smaller(pivot, number, conn, inactive_clients):
list2.append(number)
else:
list1.append(number)
return semiQuick(list1, conn, inactive_clients) + semiQuick(list2, conn, inactive_clients)