The Divide and conquer approach-merge sort

Source: Internet
Author: User

1 The divide  and conquer approach-  Merge sort
2 The theory of merging sort is called divide-and-conquer method.3 The idea of divide-and-conquer method is to decompose the problem into several sub-problems, which are smaller in size and similar to the original one .4 then recursion (recursive) solves these sub-problems, and finally merges the solutions of these sub-problems to obtain5 solution to the original problem.6That is, decomposition--merge.7 8The Divide andConquer approach9Decomposition: The sequence of n elements to be sorted is decomposed into two n/2 with atwo sub-sequences.Ten Workaround: Sort two sub-sequences recursively by using merge sort. One Merge: Merges two sorted sub-sequences to derive results. A -The merge sort algorithm'Complexity of Time'It's Nlogn . - the ImportTime , Random - - defSortdivide (alist):#decomposition divide - ifLen (alist) <= 1: + returnalist -L1 = Sortdivide (alist[:alist.__len__()//2]) +L2 = Sortdivide (alist[alist.__len__()//2:]) A returnSortmerge (L1,L2) at - defSortmerge (L1, L2):#Resolve & Merge Sort & Merge -ListS = [] - Print("Left -", L1) - Print("Right -", L2) -I,j =0,0 in whileI < L1.__len__() andJ < L2.__len__(): - ifL1[i] <=L2[j]: to lists.append (L1[i]) +i + = 1 - Print("- I.", i) the Else: * lists.append (L2[j]) $J + = 1Panax Notoginseng Print("-j", J) - Print(ListS) the Else: + ifi = = L1.__len__(): A lists.extend (l2[j:]) the Else: + lists.extend (l1[i:]) - Print(ListS) $ Print("Product-", ListS) $ returnListS - - defrandomlist (n,r): theF =0 -Rlist = []Wuyi whileF <N: theF + = 1 - rlist.append (Random.randrange (0,r)) Wu returnrlist - About if __name__=="__main__": $Alist = Randomlist (9,100) - Print("List-o", Alist) -Startt =time.time () - Print("list-s", Sortdivide (alist)) AEndt =time.time () + Print("Time Elapsed:", Endt-Startt) the - output, $List-o [88, 79, 52, 78, 0, 43, 21, 55, 62] theLeft-[88] theRight-[79] the-j 1 the[79] -[79, 88] inProduct-[79, 88] theLeft-[52] theRight-[78] About-I. 1 the[52] the[52, 78] theProduct-[52, 78] +Left-[79, 88] -Right-[52, 78] the-j 1Bayi[52] the-j 2 the[52, 78] -[52, 78, 79, 88] -Product-[52, 78, 79, 88] theLeft-[0] theRight-[43] the-I. 1 the [0] -[0, 43] theProduct-[0, 43] theLeft-[55] theRight-[62]94-I. 1 the[55] the[55, 62] theProduct-[55, 62]98Left-[21] AboutRight-[55, 62] --I. 1101[21]102[21, 55, 62]103Product-[21, 55, 62]104Left-[0, 43] theRight-[21, 55, 62]106-I. 1107 [0]108-j 1109[0, 21] the-I. 2111[0, 21, 43] the[0, 21, 43, 55, 62]113Product-[0, 21, 43, 55, 62] theLeft-[52, 78, 79, 88] theRight-[0, 21, 43, 55, 62] the-j 1117 [0]118-j 2119[0, 21] --j 3121[0, 21, 43]122-I. 1123[0, 21, 43, 52]124-j 4 the[0, 21, 43, 52, 55]126-j 5127[0, 21, 43, 52, 55, 62] -[0, 21, 43, 52, 55, 62, 78, 79, 88]129Product-[0, 21, 43, 52, 55, 62, 78, 79, 88] thelist-s [0, 21, 43, 52, 55, 62, 78, 79, 88]131Time elapsed:0.0010027885437011719

The Divide and conquer approach-merge 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.