Use python to implement 8 sorting algorithms-Hill Sorting

Source: Internet
Author: User
The basic idea of Hill Sorting: Hill sorting is based on the improvement of insert sorting, because the insert sorting is efficient for sorted series operations, but insert sorting is generally inefficient, because only one digit can be moved at a time. Therefore, the Hill sort is first sorted by group until the group increment is 1. Example: arr [,]. when the group increment is 5, the red number is a group, which is inserted in the order and cyclically traversed by arr [13, 3... the basic idea of Hill sorting:

Hill sorting is based on the improvement of insert sorting. because insert sorting is efficient for sorted series operations, insert sorting is generally inefficient because only one bit can be moved at a time. Therefore, the Hill sort is first sorted by group until the group increment is 1.

Example:

Arr = [,]. when the group increment is 5, the red number is a group, which is sorted by insertion and cyclically traversed in turn.

Arr = [,]. after traversal is completed, the group is incrementally auto-subtracted,

Arr = [,], and then insert and sort groups whose group increment is 2 until the group increment is 1.

Code:

Python code

Def shell_sort (lists ):

# Hill Sorting

Count = len (lists)

Step = 2

Group = count/step

While group> 0: # loop through group incremental grouping

For I in range (0, group ):

J = I + group

While j <count: # index of the key value in the group, incremental auto-increment

K = j-group

Key = lists [j]

While k> = 0: # Sort inserts in groups

If lists [k]> key:

Lists [k + group], lists [k] = lists [k], key

Else: break

K-= group

J + = group

Group/= step

Return lists

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.