Skip to content

Optimization for shell sort #4038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 18, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions sorts/shell_sort.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
"""
This is a pure Python implementation of the shell sort algorithm

For doctests run following command:
python -m doctest -v shell_sort.py
or
python3 -m doctest -v shell_sort.py

For manual testing run:
python shell_sort.py
https://en.wikipedia.org/wiki/Shellsort#Pseudocode
Comment on lines 1 to +2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave the first line like this

Suggested change
"""
This is a pure Python implementation of the shell sort algorithm
For doctests run following command:
python -m doctest -v shell_sort.py
or
python3 -m doctest -v shell_sort.py
For manual testing run:
python shell_sort.py
https://en.wikipedia.org/wiki/Shellsort#Pseudocode
"""
This is a Python implementation of the shell sort algorithm.
Reference: https://en.wikipedia.org/wiki/Shellsort#Pseudocode

"""


Expand All @@ -25,15 +17,16 @@ def shell_sort(collection):
[-45, -5, -2]
"""
# Marcin Ciura's gap sequence

gaps = [701, 301, 132, 57, 23, 10, 4, 1]
for gap in gaps:
for i in range(gap, len(collection)):
insert_value = collection[i]
j = i - gap
while j >= 0 and insert_value < collection[j]:
collection[j + gap] = collection[j]
j = i
while j >= gap and insert_value < collection[j - gap]:
collection[j] = collection[j - gap]
j -= gap
collection[j + gap] = insert_value
collection[j] = insert_value
return collection


Expand Down