-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_sort.py
40 lines (31 loc) · 1.22 KB
/
merge_sort.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
def merge_sort(individuals_fitness_dict):
def merge(left, right):
if len(left) == 0:
return right
if len(right) == 0:
return left
result = []
index_left = index_right = 0
while len(result) < len(left) + len(right):
if individuals_fitness_dict[left[index_left]] >= individuals_fitness_dict[right[index_right]]:
result.append(left[index_left])
index_left += 1
else:
result.append(right[index_right])
index_right += 1
if index_right == len(right):
result += left[index_left:]
break
if index_left == len(left):
result += right[index_right:]
break
return result
def start_merge(individuals):
if len(individuals) < 2:
return individuals
midpoint = len(individuals) // 2
return merge(
left=start_merge(individuals[:midpoint]),
right=start_merge(individuals[midpoint:]))
individuals = list(individuals_fitness_dict.keys())
return start_merge(individuals)