MCPcopy Create free account
hub / github.com/geekcomputers/Python / shellSort

Function shellSort

Sorting Algorithms/Shell Sort.py:4–28  ·  view source on GitHub ↗
(arr)

Source from the content-addressed store, hash-verified

2
3
4def shellSort(arr):
5 # Start with a big gap, then reduce the gap
6 n = len(arr)
7 gap = n / 2
8
9 # Do a gapped insertion sort for this gap size.
10 # The first gap elements a[0..gap-1] are already in gapped
11 # order keep adding one more element until the entire array
12 # is gap sorted
13 while gap > 0:
14 for i in range(gap, n):
15 # add a[i] to the elements that have been gap sorted
16 # save a[i] in temp and make a hole at position i
17 temp = arr[i]
18
19 # shift earlier gap-sorted elements up until the correct
20 # location for a[i] is found
21 j = i
22 while j >= gap and arr[j - gap] > temp:
23 arr[j] = arr[j - gap]
24 j -= gap
25
26 # put temp (the original a[i]) in its correct location
27 arr[j] = temp
28 gap /= 2
29
30
31# Driver code to test above

Callers 1

Shell Sort.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected