Insert sort for decimal groups in merge sort

Source: Internet
Author: User
The complexity of a pure merge sort is: O (NLGN), whereas the time complexity of a pure insert sort is: O (n^2). Merge sort when data volume is large

But inserting a sort may run faster when n is smaller. Therefore, when the sub-problem becomes enough in the merge sort, the insertion sort is used to make the recursive leaves thicker, which can speed up the sorting. So how is this small enough to measure? Please see below:

So many I don't prove it, it's easier:

A, insert sort worst case can be ordered in O (NK) time each n/k sub-list of length K

B, in the worst case, these sub-tables can be combined in O (NLG (n/k)) time

C, the worst case run time complexity of the revised algorithm is O (nk + NLG (n/k))

Well, O (NK+NLG (n/k)) =o (NLGN). Only the maximum is K=o (LGN). The first item on the left side of the equation is a high-order item. If k is greater than LGN, it is more complex than merge sort. The left can be written as nk+nlgn-nlgk,k equals LGN, is 2nlgn-nlglgn. Ignoring the constant coefficients is the same as merging the sort.

Final conclusion: When K < LG (n) is used, insert sort

From At003_insertsort import insertsortfrom math Import log__author__ = ' Xiong Neng ' def mergesort (seq): Mergesortrange ( Seq, 0, Len (seq)-1, log (len)-1) def mergeorderedseq (seq, left, middle, right): "" "seq: to sort the sequence left <= Middle <= Right Sub-array Seq[left:  Middle] and Seq[middle+1..right] are ordered by the time complexity of the sort is O (n) "" "TEMPSEQ = [] I = left j = middle + 1 while I <=             Middle and J <= Right:if Seq[i] <= seq[j]: tempseq.append (seq[i)) i + = 1 Else: Tempseq.append (Seq[j]) J + = 1 if I <= middle:tempSeq.extend (Seq[i:middle + 1]) Els E:tempseq.extend (Seq[j:right + 1]) Seq[left:right + 1] = Tempseq[:]def mergesortrange (seq, start, end, Threshol d): "" "Merges the subsequence of a sequence start: The start subscript of the sub-sequence end: The end subscript of the subsequence threshold: to sort the length below this value, insert sort" "" if End-sta RT < THRESHOLD:TEMPSEQ = Seq[start:end + 1] insertsort (TEMPSEQ) Seq[start:end + 1] = tempseq[:] Elif Start < end: # If Start >= end terminates recursive call middle = (start + end)/2 Mergesortrange (SE Q, Start, middle, threshold) # line up the left half of the Mergesortrange (SEQ, middle + 1, end, Threshold) # and then half the right side Mergeord Eredseq (seq, start, Middle, end) # final Merge sort result if __name__ = = ' __main__ ': AA = [4, 2, 5, 1, 6, 3, 7, 9, 8] mergesort (AA ) Print (AA)
  • 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.