This article describes how to implement the merge sorting algorithm in Python Programming. The average time complexity of the merge sorting algorithm is (n \ logn). For details, refer to the following basic ideas: merge sorting is a typical sub-division idea. it splits an unordered list into two parts and splits each sub-sequence into two parts until Division is impossible. Then, we start the merging process, compare each sub-sequence with the elements of another sub-sequence, and merge the small elements into the result sequence in sequence to complete the merging and sorting.
Merge operation process:
Apply for a space, so that the size is the sum of two sorted sequences. This space is used to store the merged sequence.
Set two pointers. The initial position is the start position of the two sorted sequences respectively.
Compare the elements pointed to by the two pointers, select a relatively small element into the merging space, and move the pointer to the next position.
Repeat step 3 until a pointer reaches the end of the sequence
Copy all the remaining elements of another sequence directly to the end of the merged sequence.
The above statement is a theoretical statement. The following is an actual example:
For example, an unordered array
[6,2,3,1,7]
First, the array is decomposed recursively:
[6],[2],[3],[1],[7]
Then begin to merge and sort the data, which is also performed recursively:
The two are sorted by merge:
[2,6],[1,3],[7]
In the previous step, the process is actually merged in this step, but the process cannot be fully displayed due to the number of items in each list. The following shows the process.
Initial:
a = [2,6] b = [1,3] c = []
Step 2: extract a number from a and B in sequence: 2, 1 compare the size and put it in c. Then, delete the number from the original list. The result is:
a = [2,6] b = [3] c = [1]
Step 2: Continue to retrieve the number from a and B in sequence, that is, repeat the above steps. this time: compare the size of 2 and 3 and put the number in c, and delete the number from the original list, the result is:
a = [6] b = [3] c = [1,2]
In step 2, repeat the front step. The result is:
a = [6] b = [] c = [1,2,3]
In the last step, append 6 to c, and the result is:
a = [] b = [] c = [1,2,3,6]
The preceding process is repeatedly applied to merge [1, 2, 3, 6] and [7 ].
The sorting result is obtained.
[1,2,3,6,7]
This article lists three python implementation methods:
Method 1: translate the process described above. it's a bit dumb.
#! /Usr/bin/env python # coding: utf-8def merge_sort (seq): if len (seq) = 1: return seq else: middle = len (seq) /2 left = merge_sort (seq [: middle]) right = merge_sort (seq [middle:]) I = 0 # left count j = 0 # right count k = 0 # total count while I <len (left) and j <len (right ): if left [I] <right [j]: seq [k] = left [I] I + = 1 k + = 1 else: seq [k] = right [j] j + = 1 k + = 1 remain = left if I
Method 2: the list. pop () method is applied to obtain values in order, and the code is more compact and concise.
#! /Usr/bin/env python # coding: utf-8def merge_sort (lst): # this method comes from Wikipedia if len (lst) <= 1: return lst def merge (left, right): merged = [] while left and right: merged. append (left. pop (0) if left [0] <= right [0] else right. pop (0) while left: merged. append (left. pop (0) while right: merged. append (right. pop (0) return merged middle = int (len (lst)/2) left = merge_sort (lst [: middle]) right = merge_sort (lst [middle:]) return merge (left, right)
Method 3: The Method of merging and sorting is provided in the python module heapq. you only need to import the decomposed results to this method.
#! /usr/bin/env python#coding:utf-8from heapq import mergedef merge_sort(seq): if len(seq) <= 1: return m else: middle = len(seq)/2 left = merge_sort(seq[:middle]) right = merge_sort(seq[middle:]) return list(merge(left, right)) #heapq.merge()if __name__=="__main__": seq = [1,3,6,2,4] print merge_sort(seq)
For more information about how to implement the merge sorting algorithm in Python programming, see The PHP Chinese network!