算法学习笔记--5.merge sort & divide-and-conquer
2017-08-10 12:15
405 查看
上回用到了一个选择排序:
用上昨天的算法复杂度,可以看出,选择排序的算法复杂度为O(n2) ,n=len(lists).
即最坏情况下,算法运行(len(lists))2步可以得到结果。
改进一下,用归并排序(分而治之的思想)。
Fortunately, we can do a lot better than quadratic time using a technique similar to that for binary search. As we have said, binary search is an example of a divide-and-conquer algorithm.
In general, a divide and conquer algorithm is characterized by:
A threshold input size, below which the problem size is not subdivided.
The size and number of sub-instances into which an instance is split.
The algorithm used to combine sub-solutions.
Like many divide-and-conquer algorithms, merge sort is most easily deacribed recursively:
If the list is length 0 or 1, it is already sorted.
If the list has more than one element, split the list into two lists, and use merge sort to sort each of them.
Merge the results.
在看这个归并排序排序的算法复杂度。
merge_sort(L)部分算法复杂度为O(log(n)),或者说log2(n),只用乘一个常数log2(10)。
merge 部分算法复杂度为O(len(L))。
整个归并排序算法复杂度为O(nlog(n)),n=len(L)
n = 5, 即要排序的列表长度为5。
选择排序最多需要运行25步。
归并排序最多需要运行十几步。
n = 1000,选择排序最多需要运行1000000步,而归并排序最多需要运行不到10000步。
用上昨天的算法复杂度,可以看出,选择排序的算法复杂度为O(n2) ,n=len(lists).
即最坏情况下,算法运行(len(lists))2步可以得到结果。
>>> def sel_sort(lists): for i in range(len(lists)): for j in range(i + 1, len(lists)): if lists[i] > lists[j]: lists[i], lists[j] = lists[j], lists[i] return lists
改进一下,用归并排序(分而治之的思想)。
Fortunately, we can do a lot better than quadratic time using a technique similar to that for binary search. As we have said, binary search is an example of a divide-and-conquer algorithm.
In general, a divide and conquer algorithm is characterized by:
A threshold input size, below which the problem size is not subdivided.
The size and number of sub-instances into which an instance is split.
The algorithm used to combine sub-solutions.
Like many divide-and-conquer algorithms, merge sort is most easily deacribed recursively:
If the list is length 0 or 1, it is already sorted.
If the list has more than one element, split the list into two lists, and use merge sort to sort each of them.
Merge the results.
def merge_sort(L): if len(L) < 2: return L else: mid = int(len(L)/2) left = merge_sort(L[:mid]) right = merge_sort(L[mid:]) return merge(left, right)
def merge(L1,L2): i, j = 0, 0 result = [] while i < len(L1) and j < len(L2): if L1[i] < L2[j]: result.append(L1[i]) i += 1 else: result.append(L2[j]) j += 1 result += L1[i:] result += L2[j:] return result
在看这个归并排序排序的算法复杂度。
merge_sort(L)部分算法复杂度为O(log(n)),或者说log2(n),只用乘一个常数log2(10)。
merge 部分算法复杂度为O(len(L))。
整个归并排序算法复杂度为O(nlog(n)),n=len(L)
n = 5, 即要排序的列表长度为5。
选择排序最多需要运行25步。
归并排序最多需要运行十几步。
n = 1000,选择排序最多需要运行1000000步,而归并排序最多需要运行不到10000步。
相关文章推荐
- 算法学习笔记--2. Selection sort & Fibonacci sequence
- 算法上机题目mergesort,priority queue,Quicksort,divide and conquer
- 九章算法系列(#3 Binary Tree & Divide Conquer)-课堂笔记
- Algorithms Review: Divide and Conquer(Binary Search & Merge Sort)
- 排序算法-10-算法-分治法(Divide and Conquer)
- 【算法】分而治之(DivideAndConquer) -- C++源代码(VS2015)
- 算法学习----->Insertsort(插入排序)
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose)【莫队算法裸题&&学习笔记】
- C++ Primer 学习笔记:泛型算法入门 插入迭代器 && iostream迭代器 && 反向迭代器
- 【算法】【Divide and conquer】Median of Two Sorted Arrays
- 学习笔记之快速排序——quicklySort——基础算法——java
- 数据结构&算法学习笔记: 归并排序
- 算法第二周: Divide and Conquer-分治算法
- 算法学习笔记之——priority queue、heapsort、symbol table、binary search trees
- 归并排序(mergeSort) 与 分而治之策略(divide and conquer)
- 算法学习笔记--3.Binary Search & Algorithmic Complexity
- 算法入门(1)——分治算法(Divide and Conquer)
- 【算法】【Divide and conquer】Different Ways to Add Parentheses
- 数据结构&算法学习笔记: 快速排序
- 回归&logistics回归笔记-七月算法(julyedu.com)4 月机器学习算法班学习笔记