forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell_sort.py
55 lines (41 loc) · 1.34 KB
/
shell_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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""Implementacao do algoritmo shell sort"""
def shell_sort(a_list):
"""
Algoritmo shell sort.
Argumentos:
a_list: list. Uma lista nao ordenada
"""
sublist_count = len(a_list) // 2
while sublist_count > 0:
for start_position in range(sublist_count):
gap_insertion_sort(a_list, start_position, sublist_count)
sublist_count = sublist_count // 2
def gap_insertion_sort(a_list, start, gap):
"""Algoritmo gap insertion sort."""
for i in range(start + gap, len(a_list), gap):
current_value = a_list[i]
position = i
while position >= gap and a_list[position - gap] > current_value:
a_list[position] = a_list[position - gap]
position = position - gap
a_list[position] = current_value
some_list = [54, 26, 93, 17, 77, 31, 44, 55, 20]
shell_sort(some_list)
print(some_list)
def shell(seq):
"""
Algoritmo de ordenacao shell
Argumentos:
seq: list. lista nao ordenada
"""
inc = len(seq) // 2
while inc:
for i, element in enumerate(seq):
while i >= inc and seq[i - inc] > element:
seq[i] = seq[i - inc]
i -= inc
seq[i] = element
inc = 1 if inc == 2 else int(inc * 5.0 / 11)
data = [22, 7, 2, -5, 8, 4]
shell(data)
print(data) # [-5, 2, 4, 7, 8, 22]