Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 715a13a

Browse files
Shell Sort
1 parent 76e2cf2 commit 715a13a

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

‎Sorting-Algo/shellsort.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
Shell Sort
3+
Shell sort is a generalized version of the insertion sort algorithm. It first sorts elements that are far apart from each other and successively reduces the interval between the elements to be sorted.
4+
5+
The interval between the elements is reduced based on the sequence used. Some of the optimal sequences that can be used in the shell sort algorithm are:
6+
7+
Shell's original sequence: N/2 , N/4 , ..., 1
8+
Knuth's increments: 1, 4, 13, ..., (3k – 1) / 2
9+
Sedgewick's increments: 1, 8, 23, 77, 281, 1073, 4193, 16577...4j+1+ 3·2j+ 1
10+
Hibbard's increments: 1, 3, 7, 15, 31, 63, 127, 255, 511...
11+
Papernov & Stasevich increment: 1, 3, 5, 9, 17, 33, 65,...
12+
Pratt: 1, 2, 3, 4, 6, 9, 8, 12, 18, 27, 16, 24, 36, 54, 81....
13+
'''
14+
def shellSort(list, n):
15+
16+
# Rearrange elements at each n/2, n/4, n/8, ... intervals
17+
interval = n // 2
18+
while interval > 0:
19+
for i in range(interval, n):
20+
temp = list[i]
21+
j = i
22+
while j >= interval and list[j - interval] > temp:
23+
list[j] = list[j - interval]
24+
j -= interval
25+
26+
list[j] = temp
27+
interval //= 2
28+
29+
30+
data = [9, 8, 3, 7, 5, 6, 4, 1]
31+
length = len(data)
32+
shellSort(data, length)
33+
print(data) #[1, 3, 4, 5, 6, 7, 8, 9]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /