-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShellSort.py
58 lines (52 loc) · 2.35 KB
/
ShellSort.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
"""
Shell Sort
Shell sort is an optimization over Insertion sort
Aim is to reduce swaps and comparisons by swapping greater elements at right side of array and light element at left
Algorithm
Start with gap = n/2 and sort sub arrays
Keep reducing gap by n/2 and keep on sorting sub arrays
Last iteration should have gap = 1. At this point it is same as insertion sort
Actually, we are doing insertion sort but with some optimization
so that number of swaps and comparisons can be reduced
Disadvantage of Insertion sort over Shell Sort
When small elements are towards the end of array it takes many:
Comparisons
Swaps
Worst-case Performance
O(n^2) (worst known worst-case gap sequence)
O(n log^2(n)) (best known worst-case gap sequence)
Best-case Performance
O(n log n) (most gap sequences)
O(n log^2(n)) (best known worst-case gap sequence)
"""
def ShellSort(arr):
# gap = 3 # initialised gap to 3
size = len(arr)
gap = size // 2 # initialised gap to half of size of array
while gap > 0:
for i in range(gap, size):
pointer = arr[i] # pointer initialised to array such that gap is maintained
j = i
# get element which is at gap 3 and compare with current pointer element AND j>=gap else (j-gap) would be negative
while j >= gap and arr[j - gap] > pointer:
arr[j] = arr[j-gap] # swapped elements
j -= gap # In each while loop iteration, reduce by gap for comparison with previous elements
arr[j] = pointer # after while loop ends, all elements have been swapped
# after function end, all heavy values are at right side and light values at left of partially-sorted array
gap = gap // 2 # gap reduced by half
if __name__ == '__main__':
elements = [21, 38, 29, 17, 4, 25, 11, 32, 9]
# Partially sorted array after Shell Sort[11, 4, 9, 17, 32, 25, 21, 38, 29]
ShellSort(elements)
print(elements)
# Test Cases
tests = [
[89, 78, 61, 53, 23, 21, 17, 12, 9, 7, 6, 2, 1], # DESC sorted array
[], # empty array
[1, 5, 8, 9], # sorted array
[234, 3, 1, 56, 34, 12, 9, 12, 1300], # array with uneven elements
[5] # array with single element
]
for elements in tests:
ShellSort(elements)
print(elements)