-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell_sort.py
28 lines (25 loc) · 977 Bytes
/
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
"""
|-----------------------------------------------------------|
| Shell Sort |
|-----------------------------------------------------------|
|Type || In-place comparison sort |
|Worst case performance || O(n2) |
|Best case performance || O(n log2 n) |
|Average case performance || depends on gap sequence |
|Worst case space complexity || O(n) total, O(1) auxiliary |
|-----------------------------------------------------------|
"""
from decorators import timethis
@timethis
def shell_sort(array):
mid = len(array) / 2
while mid:
for i in range(len(array)):
j = i
temp = array[i]
while j >= mid and array[j-mid] > temp:
array[j] = array[j - mid]
j -= mid
array[j] = temp
mid = mid/2 if mid/2 else (0 if mid == 1 else 1)
return array