分治排序算法的python实现
2014-03-31 16:01
232 查看
时间复杂度O(NlgN)
分治模式的一般步骤:
分解(Divide):将原问题分解成一些列子问题;
解决(Conquer):递归地解各子问题,若子问题足够小,则直接求解;
合并(Combine):将子问题的结果合并成原问题的解。
以下是分治排序的python实现:
enjoy it.
分治模式的一般步骤:
分解(Divide):将原问题分解成一些列子问题;
解决(Conquer):递归地解各子问题,若子问题足够小,则直接求解;
合并(Combine):将子问题的结果合并成原问题的解。
以下是分治排序的python实现:
def merge(p_list,p_start,p_mid,p_end): left_list = p_list[p_start:p_mid+1]; right_list = p_list[p_mid+1:p_end+1]; left_index = right_index = 0; for i in xrange(p_start,p_end+1): if left_index >= len(left_list) or right_index >= len(right_list): break; if left_list[left_index] <= right_list[right_index]: p_list[i] = left_list[left_index]; left_index += 1; else: p_list[i] = right_list[right_index]; right_index += 1; if left_index >= len(left_list): p_list[i:p_end+1] = right_list[right_index:len(right_list)]; else: p_list[i:p_end+1] = left_list[left_index:len(left_list)]; def merge_sort(p_list,p_start,p_end): if p_start < p_end: mid = (p_start + p_end) >> 1; merge_sort(p_list,p_start,mid); merge_sort(p_list,mid+1,p_end); merge(p_list,p_start,mid,p_end);
enjoy it.
相关文章推荐
- 八种排序算法 Java、Python、C++实现 -- 插入排序
- 常见排序算法-Python实现
- 排序算法Python实现
- Python2实现的几种排序算法
- 部分排序算法python实现
- [面试笔记] 各种排序算法的Python实现以及结果可视化
- python实现2种简单的排序算法--冒泡排序和选择排序
- 简单排序算法的python实现
- python排序算法的实现-选择
- 使用python实现排序算法(Insertion Sort)
- Python实现的几种排序算法
- 【排序】用Python实现八大排序算法--堆排序
- python实现快速排序算法
- Python排序算法的实现
- 八大排序算法的 Python 实现
- 排序算法—直接插入排序算法分析与实现(Python)
- 用 python 实现各种排序算法
- 如何用Python实现八大排序算法
- Python笔记-排序算法实现(1)
- python实现各种排序算法