Python implements eight sorting algorithms (2) and python implements eight sorting algorithms
This article is followed by the eight sorting algorithms (part1) implemented by python in the previous blog. We will continue to use python to implement the remaining four of the eight sorting algorithms: Fast sorting, heap sorting, Merge Sorting, and base sorting.
5. Fast sorting
Quick sorting is generally considered to have the best average performance in sorting methods of the same order of magnitude (O (nlog2n.
Algorithm idea:
A group of unordered data a [1], a [2],... A [n], which needs to be arranged in ascending order. First, use data a [x] as the benchmark. Compare and sort a [x] with other data so that a [x] is ranked at the k bit of the data and a [1] ~ Each data in a [k-1] <a [x], a [k + 1] ~ Each data in a [n] is> a [x], and then a [1] to A [k-1] And a [k + 1] ~ A [n] groups of data are sorted quickly.
Advantage: extremely fast, with less data movement;
Disadvantage: unstable.
Python code implementation:
Def quick_sort (list): little = [] export tlist = [] large = [] # recursive exit if len (list) <= 1: return list else: # use the first value as the benchmark value = list [0] for I in list: # place a value smaller than the benchmark value in the less series if I <small: little. append (I) # place a value greater than the benchmark value in the more series elif I> values: large. append (I) # Save the same value as the benchmark value in the benchmark series else: pivotList. append (I) # Sort less series and more series quickly. little = quick_sort (little) large = quick_sort (large) return little + wide tlist + large
The following code comes from three lines in the second version of Python cookbook to implement quick python sorting.
#! /Usr/bin/env python # coding: UTF-8 ''' file: python-8sort.pydate: 9/1/17 AMauthor: lockeyemail: lockey@123.comdesc: python implements the eight sorting algorithms '''lst = [65,568, 23,] def quick_sort (list): if len (list) <= 1: return list else: rows = list [0] return quick_sort ([x for x in list [1:] if x <rows]) + \ [history] + \ quick_sort ([x for x in list [1:] if x> = history])
Test result:
Well, there is also a more concise syntax sugar, and one line is complete:
Quick_sort = lambda xs: (len (xs) <= 1 and [xs]) or [quick_sort ([x for x in xs [1:] if x <xs [0]) + [xs [0] + quick_sort ([x for x in xs [1:] if x> = xs [0]) [0]
If the initial sequence is ordered by key code or is basically ordered, the fast sequence is sorted by bubble instead. For improvement, the benchmark record is usually selected based on "Three take China and France", that is, the center of the key codes of the two endpoints and the midpoint of the sorting interval is adjusted to the pivot record. Quick sorting is an unstable sorting method.
In the improved algorithm, we will recursively call quick sorting for subsequences with a length greater than k, so that the original sequence is basically ordered, and then sort the entire basic sequence by inserting the sorting algorithm. Practice has proved that the time complexity of the improved algorithm is reduced, and when k is about 8, the improved algorithm has the best performance.
6. Heap Sort)
Heap sorting is a kind of tree-based sorting that effectively improves the Direct selection and sorting.
Advantage: High Efficiency
Disadvantage: unstable
Defined by heap: sequences with n elements (h1, h2 ,..., hn), if and only if (hi> = h2i, hi> = 2i + 1) or (hi <= h2i, hi <= 2i + 1) (I = 1, 2 ,..., n/2) is called heap. Here we only discuss the heap that meets the conditions of the former. From the definition of heap, we can see that the heap top element (that is, the first element) must be a heap top element (large top heap ). A fully binary tree can intuitively represent the heap structure. The heap top is the root, and the others are the left and right subtree.
Algorithm idea:
Initially, the sequence of the numbers to be sorted is regarded as a binary tree for sequential storage, and their storage order is adjusted to make it a heap. At this time, the root node of the heap has the largest number. Then, the root node is exchanged with the last node in the heap. Then adjust the preceding (n-1) number to make it heap. Wait until there are only two nodes in the heap and exchange them. Finally, an ordered sequence of n nodes is obtained. According to the algorithm description, heap sorting requires two processes: creating a heap and switching the last element of the heap. Therefore, heap sorting consists of two functions. One is the heap build penetration function, and the other is the function that calls the penetration function repeatedly to implement sorting.
Python code implementation:
#-*-Coding: UTF-8-*-''' Created on September 2, 2017 Running environment: win7.x86 _ 64 eclipse python3 @ author: Lockey ''' lst = [65,568, 23, 34, 65,8, 6,9] def adjust_heap (lists, I, size): # adjust heap lchild = 2 * I + 1; rchild = 2 * I + 2 max = I if I <size/2: if lchild <size and lists [lchild]> lists [max]: max = lchild if rchild <size and lists [rchild]> lists [max]: max = rchild if max! = I: lists [max], lists [I] = lists [I], lists [max] adjust_heap (lists, max, size) def build_heap (lists, size ): # create a heap halfsize = int (size/2) for I in range (0, halfsize) [:-1]: adjust_heap (lists, I, size) def heap_sort (lists): # heap sorting size = len (lists) build_heap (lists, size) for I in range (0, size) [:-1]: lists [0], lists [I] = lists [I], lists [0] adjust_heap (lists, 0, I) print (lists)
Result example:
7. Merge and sort
Algorithm idea:
Merge Sorting is an effective Sorting Algorithm Based on the Merge operation. This algorithm is a typical application of divide and conquer. Merges ordered subsequences to obtain a fully ordered sequence. That is, first orders each subsequence, and then orders the subsequence segments. If two ordered tables are merged into an ordered table, it is called a two-way merge.
The merging process is: Compare the sizes of a [I] And a [j]. If a [I] is less than or equal to a [j], copy the element a [I] In the first ordered table to r [k] and Add 1 to I and k respectively; otherwise, copy the element a [j] In the second ordered table to r [k], and Add 1 to j and k respectively. In this way, the loop continues until one of the ordered tables is completed, then copy the remaining elements in another ordered table to the Unit from subscript k to subscript t in r. The Merge Sorting Algorithm is implemented recursively. We first sort the subintervals [s, t] to the midpoint, then sort the left subintervals, and then sort the right subintervals, finally, merge the left and right intervals into an ordered interval [s, t] with one merge operation.
#-*-Coding: UTF-8-*-''' Created on September 2, 2017 Running environment: win7.x86 _ 64 eclipse python3 @ author: Lockey ''' lst = [65,568, 23, 34, 65,8, 6,9] def merge (left, right): I, j = 0, 0 result = [] while I <len (left) and j <len (right ): if left [I] <= right [j]: result. append (left [I]) I + = 1 else: result. append (right [j]) j + = 1 result + = left [I:] result + = right [j:] print (result) return resultdef merge_sort (lists ): # merge and sort if len (lists) <= 1: return lists num = int (len (lists)/2) left = merge_sort (lists [: num]) right = merge_sort (lists [num:]) return merge (left, right)
Sample program results:
8. Sort buckets/base (Radix Sort)
Advantage: fast, and the best efficiency can reach O (1)
Disadvantages:
1. The first is the high space complexity and the extra overhead required. There is a space overhead for sorting two arrays. One is to store the array to be sorted, and the other is the so-called bucket. For example, if the value to be sorted is from 0 m-1, m buckets are required, this bucket array requires at least m space.
2. The elements to be sorted must be within a certain range.
Algorithm idea:
Is to divide the array into a limited number of buckets. Each bucket can be sorted individually (it is possible to use another sort algorithm or to continue to use the bucket sort in a progressive manner ). Bucket sorting is an inductive result of nest sorting. When the values in the array to be sorted are evenly distributed, the linear time (n) is used for Bucket sorting )). However, bucket sorting is not a comparative sorting, and is not affected by the lower limit of O (n log n.
Simply put, data is grouped into buckets and sorted in each bucket.
For example, you want to sort n integers A [1. n] in the range [1. 1000 ].
First, you can set the bucket size to 10 so that there are 100 buckets. Specifically, set Set B [1] to store [1 .. the integer of 10], which is a set of B [2] storage (10 .. 20] integer ,...... Set the integer of B [I] storage (I-1) * 10, I * 10], I = 100. A total of 100 buckets are available.
Then, scan A [1. n] From start to end and put each A [I] in the corresponding bucket B [j. Sort the numbers in each of these 100 buckets. In this case, you can use bubble, select, or even fast sorting. Generally, any sorting method is acceptable.
Finally, the numbers in each bucket are output sequentially, and the numbers in each bucket are output from small to large. In this way, a sequence of all numbers is obtained.
Suppose there are n numbers and m buckets. If the numbers are evenly distributed, there are n/m numbers on average in each bucket. If
The complexity of the entire algorithm is
O (n + m * n/m * log (n/m) = O (n + nlogn-nlogm)
From the above formula, when m is close to n, the complexity of Bucket sorting is close to O (n)
Of course, the above complexity calculation is based on the assumption that n numbers are evenly distributed. This assumption is very strong, and the results in practical applications are not so good. If all the numbers are in the same bucket, the sorting will degrade.
Python code implementation:
#-*-Coding: UTF-8-*-''' Created on September 2, 2017 Running environment: win7.x86 _ 64 eclipse python3 @ author: Lockey ''' import mathlst = [65,56, 9,23, 100, 11] # because the list data range is less than, ten buckets are used for sorting def radix_sort (lists, radix = 10 ): k = int (math. ceil (math. log (max (lists), radix) bucket = [[] for I in range (radix)] for I in range (1, k + 1 ): for j in lists: gg = int (j/(radix ** (I-1) % (radix ** I) bucket [gg]. append (j) del lists [:] for z in bucket: lists + = z del z [:] print (lists) return lists
Program Running Test Result:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.