-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshell_sort.py
More file actions
62 lines (45 loc) · 1.63 KB
/
shell_sort.py
File metadata and controls
62 lines (45 loc) · 1.63 KB
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
"""
Shell Sort Algorithm
Shell Sort is a generalization of Insertion Sort that allows the exchange
of items that are far apart. It starts by sorting pairs of elements far apart
from each other, then progressively reduces the gap between elements to be compared.
Time Complexity:
- Best Case: O(n log n)
- Average Case: O(n^1.5) - depends on gap sequence
- Worst Case: O(n²)
Space Complexity: O(1) - in-place sorting
Stable: No (may change relative order of equal elements)
"""
def shell_sort(arr):
"""
Sorts an array using the Shell Sort algorithm.
Args:
arr: List of comparable elements to be sorted (modified in-place)
Returns:
None (sorts in-place)
"""
n = len(arr)
# Start with a large gap, then reduce the gap
# Using gap sequence: n/2, n/4, n/8, ... until gap = 1
gap = n // 2
# Do a gapped insertion sort for this gap size
while gap > 0:
# Perform insertion sort for elements at gap intervals
for i in range(gap, n):
# Store current element
temp = arr[i]
# Shift earlier gap-sorted elements up until correct position
j = i
while j >= gap and arr[j - gap] > temp:
arr[j] = arr[j - gap]
j -= gap
# Place temp at its correct position
arr[j] = temp
# Reduce gap for next iteration
gap //= 2
# Example usage
if __name__ == "__main__":
numbers = [64, 34, 25, 12, 22, 11, 90]
print(f"Original array: {numbers}")
shell_sort(numbers)
print(f"Sorted array: {numbers}")