---Study on the Road (a) Python data structures and Algorithms (4)--Hill sort, merge sort

Source: Internet
Author: User

Monologue :

The hill sort is an optimized insertion sorting algorithm, and the previously learned sort is spatially used by the list itself. And the merge sort is the use of adding new space, in exchange for the reduction of time complexity. These two ideas are completely different, destined to cause the time spent and the difference in space.

Merge sorting involves recursive use, which requires understanding the essence to better understand merge ordering, and other algorithms that apply to recursion. Understand its nature in order to better apply it.

Hill sort

The Hill sort (Shell sort) is a sort of insertion. Also known as narrowing incremental sorting, is a more efficient and improved version of the direct insertion sorting algorithm. Hill Sort is a non-stable sorting algorithm. The method is due to DL. The shell was named after it was introduced in 1959. Hill sort is to group records by a certain increment of the subscript, sorting each group using the direct insertion sorting algorithm; As the increments gradually decrease, each group contains more and more keywords, when the increment is reduced to 1 o'clock, the entire file is divided into a group, the algorithm terminates.

Complexity of Time
    • Optimal time complexity: varies by step sequence
    • Worst time complexity: O (n2)
    • Stability thinking: unstable

"""Hill Sort Optimal time complexity: different for the worst time complexity depending on the step sequence: O (n*n) Stability: unstable"""Import TimeImportRandomdefShell_sort (list): N=len (list)#Initial StepGap = N//2 whileGap >0:#Insert Sort by initial step         forJinchRange (Gap, N): I=J#Insert Sort             whileI >= Gap andLIST[I-GAP] >List[i]: list[i-GAP], list[i] = List[i], list[i-Gap] I-=Gap#get a new step sizeGap = gap//2defnew_num (LIS):"""randomly generated 50 numbers added to the list"""     forIinchRange (50): J= Random.randint (0, 10000) Lis.append (j)if __name__=='__main__': First_time=time.time ()#Empty listLis = [54,26,93,17,77,31]    #random functions added to the list    #new_num (LIS)    Print(LIS)#List Sortshell_sort (LIS)Print(LIS)#End TimeLast_time =time.time ()Print("Shared%s"% (Last_time-first_time))

Merge sort

Merge sort is a very typical application of divide-and-conquer method. The idea of merging sorts is to recursively decompose the arrays and then merge the arrays.

After the decomposition of the array to a minimum, and then merge two ordered arrays, the basic idea is to compare the first number of two arrays, who is small to take who, after taking the corresponding pointer to move backward one. Then compare until an array is empty and finally copy the remainder of the other array.

Complexity of Time
    • Optimal time complexity: O (NLOGN)
    • Worst time complexity: O (NLOGN)
    • Stability: Stable
"""merge Sort optimal time complexity: O (NLOGN) The worst time complexity: O (NLOGN) Stability: Stability and other sorting differences take advantage of a new list of algorithms sorted by the elements stored in the space change time"""Import TimeImportRandomdefMerge_sort (list):"""Merge Sort"""N=len (list)ifN <= 1:        returnList#Maximum divisibleMID = N//2#Left the ordered list formed by a list of recursive interceptsLeft_list =Merge_sort (List[:mid])#Right the ordered list formed by the list of recursive interceptsRight_list =Merge_sort (list[mid:])#to create an index of left and right cursor record list valuesLeft_pointer, Right_pointer =0,0#Create a new empty listresult = []    #Loop Compare numeric size    #exit loop condition when one of the left and right cursors equals the length of the list     whileLeft_pointer < Len (left_list) andRight_pointer <Len (right_list):#determining the left and right value sizes        ifLeft_list[left_pointer] <=Right_list[right_pointer]: Result.append (Left_list[left_pointer])#each time you judge the cursor plus oneLeft_pointer + = 1Else: Result.append (Right_list[right_pointer]) Right_pointer+ = 1#Add the last value to the new listResult + =Left_list[left_pointer:] Result+=Right_list[right_pointer:]#return value    returnresultdefnew_num (LIS):"""randomly generated 50 numbers added to the list"""     forIinchRange (50): J= Random.randint (0, 100) Lis.append (j)if __name__=='__main__': First_time=time.time ()#Empty listLis = []    #random functions added to the listnew_num (LIS)Print(LIS)#List Sort    #because the merge sort finally returns a new list, the print output is the new listAlist =merge_sort (LIS)Print(alist)#End TimeLast_time =time.time ()Print("Shared%s"% (Last_time-first_time))

---Study on the Road (a) Python data structures and Algorithms (4)--Hill sort, merge sort

Related Article

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.