FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python/algorithms/sorting/shell_sort.py at master · ImadBel-code/python · GitHub
ImadBel-code
/
python
Public
forked from
AllAlgorithms/python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python
/
algorithms
/
sorting
/
shell_sort.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
35 lines (23 loc) · 768 Bytes
Breadcrumbs
python
/
algorithms
/
sorting
/
shell_sort.py
Copy path
File metadata and controls
35 lines (23 loc) · 768 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""This is a Python implementation of the shell sort algorithm
Shell sort is a variation of insertion sort. This method starts by sorting
pairs of elements far away from each other, then progressively reducing the
gap between elements to be compared.
"""
from
random
import
randint
def
shell_sort
(
arr
):
n
=
len
(
arr
)
gap
=
n
//
2
while
gap
>
0
:
for
i
in
range
(
gap
,
n
):
tmp
=
arr
[
i
]
j
=
i
while
j
>=
gap
and
arr
[
j
-
gap
]
>
tmp
:
arr
[
j
]
=
arr
[
j
-
gap
]
j
-=
gap
arr
[
j
]
=
tmp
gap
//=
2
return
arr
# Tests
if
__name__
==
'__main__'
:
print
(
shell_sort
([
randint
(
0
,
1000
)
for
_
in
range
(
10
)]))
print
(
shell_sort
([
randint
(
-
500
,
500
)
for
_
in
range
(
10
)]))
Back
|
FazBrowse Home
|
New Git URL