Skip to content

Optimization shell sort #4119

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 2 commits into from
Feb 26, 2021
Merged

Optimization shell sort #4119

merged 2 commits into from
Feb 26, 2021

Conversation

realDuYuanChao
Copy link
Member

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@ghost ghost added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Jan 14, 2021
@realDuYuanChao realDuYuanChao changed the title optimization Optimization shell sort Jan 14, 2021
Copy link
Member

@poyea poyea left a comment

Choose a reason for hiding this comment

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

It doesn't quite seem to me that this is an optimization. May I know the reasoning behind this?

@realDuYuanChao
Copy link
Member Author

realDuYuanChao commented Jan 14, 2021

It doesn't quite seem to me that this is an optimization. May I know the reasoning behind this?

If array is sorted already. It's no need to execute assignment statement. See this

@realDuYuanChao realDuYuanChao requested a review from poyea January 20, 2021 03:38
@poyea
Copy link
Member

poyea commented Jan 20, 2021

@shellhub I've done a simple benchmark for this on random and sorted lists. I'd say they're similar in performance... Can you illustrate with an example in the test & comment: like

# consider test case [xxx]

@stale stale bot added the stale Used to mark an issue or pull request stale. label Feb 21, 2021
@realDuYuanChao realDuYuanChao removed the stale Used to mark an issue or pull request stale. label Feb 22, 2021
@TheAlgorithms TheAlgorithms deleted a comment from stale bot Feb 22, 2021
Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

@shellhub Please create a timeit (or similar) benchmark that proves that the proposed change delivers measurable performance improvement.

@ghost ghost added awaiting changes A maintainer has requested changes to this PR and removed awaiting reviews This PR is ready to be reviewed labels Feb 22, 2021
@realDuYuanChao
Copy link
Member Author

def shell_sort_slow(collection):
    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
            while j >= gap and collection[j - gap] > insert_value:
                collection[j] = collection[j - gap]
                j -= gap
            collection[j] = insert_value
    return collection


def shell_sort_faster(collection):
    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
            while j >= gap and collection[j - gap] > insert_value:
                collection[j] = collection[j - gap]
                j -= gap
            if j != i:
                collection[j] = insert_value
    return collection


def benchmark():
    from timeit import timeit
    print("shell_sort_slow", timeit("z.shell_sort_slow(z.nums)", setup="import __main__ as z"))
    print("shell_sort_faster", timeit("z.shell_sort_faster(z.nums)", setup="import __main__ as z"))


if __name__ == "__main__":
    nums = [i for i in range(1, 1000)]
    benchmark()

output:

('shell_sort_slow', 16.11249089241028)
('shell_sort_faster', 13.815173864364624)

@poyea @cclauss

@realDuYuanChao
Copy link
Member Author

Please review this PR. Thanks @poyea @cclauss @dhruvmanila @mateuszz0000

@ghost ghost removed the awaiting changes A maintainer has requested changes to this PR label Feb 25, 2021
@realDuYuanChao realDuYuanChao merged commit 67b33a2 into TheAlgorithms:master Feb 26, 2021
peRFectBeliever pushed a commit to peRFectBeliever/Python that referenced this pull request Apr 1, 2021
* optimization

* updating DIRECTORY.md

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Panquesito7 pushed a commit to Panquesito7/Python that referenced this pull request May 13, 2021
* optimization

* updating DIRECTORY.md

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
shermanhui pushed a commit to shermanhui/Python that referenced this pull request Oct 22, 2021
* optimization

* updating DIRECTORY.md

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This PR modified some existing files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants