Merge sort with python
2011-10-09 15:29
218 查看
最近在看《算法导论》,同时又在学习Python,所以想到用PYTHON实现看过的算法,来练手。
PYTHON帮忙看看有没有需要改进的写法
PYTHON帮忙看看有没有需要改进的写法
# Suppose the index of current item is i, # then the parent index is int( (i-1)/2 ), # the left child index is 2*i+1, # the right child index is 2*i+2 heap_size = int() def heapsort(A): global heap_size heap_size = 0 build_max_heap(A) for i in range(len(A)-1, 0, -1): # exchange A[0], A[i] A[0], A[i] = A[i], A[0] heap_size = heap_size-1 max_heapify(A, 0); def build_max_heap(A): global heap_size heap_size = len(A) for i in range( int( (len(A)-2)/2 ),-1,-1 ): max_heapify(A, i) def max_heapify(A, i): global heap_size l = 2*i+1 r = 2*i+2 if l <= heap_size-1 and A[l] > A[i]: largest = l else: largest = i if r <= heap_size-1 and A[r] > A[largest]: largest = r if largest != i: A[larget], A[i] = A[i], A[largest] max_heapify(A, i)
相关文章推荐
- mergesort in python
- Inversion count implemented with merge sort
- insertion mergesort bubble sort 复习 python
- 结合归并排序和插入排序 Merge with Insertion Sort
- [building block] merge sort @ Python
- [算法导论]merge sort @ Python
- 【LeetCode with Python】 Merge Two Sorted Lists
- 【LeetCode with Python】 Merge Sorted Array
- 4000 【数据结构笔记】归并排序(merge_sort)-- python2.7
- python的归并排序(mergesort)实现
- 【LeetCode with Python】 Sort Colors
- 【LeetCode with Python】 Insertion Sort List
- python mergesort 小思考,请高手指点
- 经典排序算法以及负载平衡下的平行归并排序Parallel Merge Sort with Load Balancing
- 【LeetCode with Python】 Sort List
- Quick Sort with Python
- Python中的排序方法(Bubble Sort,Insert Sort,Select Sort,Merge Sort,Quick Sort)
- MergeSort Implementation with C
- Mining Twitter Data with Python Part 3: Term Frequencies
- Using Pythonbrew and Virtualenv(with pip) for creating sandboxed Python development environments.