Python sort-Hill sort

Source: Internet
Author: User

Shell sorting uses grouping to accelerate the ordering of partial ordered arrays, and the time performance of grouping fixed-length jumping bubble hill is better than the reason of direct insertion ordering: ① The number of comparisons and moves required to insert a sort directly when the initial state of the file is basically ordered is low. ② when the n value is small, the difference between N and n^2 is also small, that is, the best time to insert the order of the complexity O (n) and the worst time complexity of 0 () is not very different. ③ in the beginning of the hill, the increment larger, more groups, the number of records in each group is small, so the direct insertion within the group is faster, and then the incremental di gradually reduced, and the number of groups gradually decreased, and the group of records gradually increased, but because already according to Di-1 as a distance arrangement, so that the file is closer to the orderly state So the new trip sort process is also faster. Therefore, the hill sort is more efficient than the direct insertion sort.
#-*-coding:utf-8-*-defShell (arr):#recursive jump, equivalent to insert shift    defStep_insert (_arr, I, h):ifI < Hor_arr[i] >= _arr[i-h]:return        Else: _arr[i], _arr[i-H] = _arr[i-h], _arr[i] Step_insert (_arr, I-h, h)#Step Size    defstep_list (_arr): _list=[] H= 1 whileH < Len (_arr)/3: _list.append (h) H= 3 * H + 1return_list[::-1]     forHinchstep_list (arr): forIinchRange (H, Len (arr)): Step_insert (arr, I, h)


Import Random
for in range (20
Print

Print arr

Python sort-Hill sort

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.