Skip to content

Commit d27c176

Browse files
authored
Create shell_sort.py
1 parent 900644a commit d27c176

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def shell_sort(arr):
2+
n = len(arr)
3+
gap = n // 2 # gap size
4+
5+
while gap > 0:
6+
for i in range(gap, n):
7+
temp = arr[i]
8+
j = i
9+
# Perform insertion sort for the elements separated by the gap
10+
while j >= gap and arr[j - gap] > temp:
11+
arr[j] = arr[j - gap]
12+
j -= gap
13+
arr[j] = temp
14+
gap //= 2
15+
16+
return arr
17+
18+
print(shell_sort([5, 8, 1, 4, 7, 9, 6, 3, 2]))

0 commit comments

Comments
 (0)