├── README.md └── src ├── 2-2.py ├── 2.1-2.py ├── 2.1-3.py ├── 2.1-4.py ├── 2.2-1.py ├── 2.2-2.py ├── 2.3-2.py ├── 2.3-4.py ├── 2.3-5.py ├── 2.3-7.py ├── 4.1-2.py ├── 6.2-2.py ├── 6.3-1.py ├── 6.5-7.py ├── 6.5.py ├── 7.1.py ├── 8.1.py └── test.py /README.md: -------------------------------------------------------------------------------- 1 | # IntroductionToAlgorithms 2 | 算法导论的所有课后题 主要python实现 3 | 4 | ###因为最近要搞算法基础,打算把算法导论重新刷一遍,其中的所有课后题都自己实现一遍,放github上记录下来 5 | -------------------------------------------------------------------------------- /src/2-2.py: -------------------------------------------------------------------------------- 1 | __author__ = 'haoxiang' 2 | 3 | def bubble_sort(sort_list): 4 | iter_len = len(sort_list) 5 | if iter_len < 2: 6 | return sort_list 7 | for i in range(iter_len-1): 8 | for j in range(iter_len-i-1): 9 | if sort_list[j] > sort_list[j+1]: 10 | sort_list[j], sort_list[j+1] = sort_list[j+1], sort_list[j] 11 | return sort_list -------------------------------------------------------------------------------- /src/2.1-2.py: -------------------------------------------------------------------------------- 1 | __author__ = 'haoxiang' 2 | 3 | 4 | def InsertSort(list): 5 | 6 | lens = len(list) 7 | for i in range(1,lens): 8 | key = list[i] 9 | j = i-1 10 | while j>=0 and list[j]0 and len(right)>0: 14 | if left[i] <= right[j]: 15 | alist[a] = left[i] 16 | i = i +1 17 | elif left[i] >= right[j]: 18 | alist[a] = right[j] 19 | j = j+1 20 | 21 | for b in range(i,len(left)): 22 | alist.append(left[b]) 23 | 24 | for c in range(j,len(right)): 25 | alist.append(left[c]) 26 | 27 | def MergeSort(alist,p,r): 28 | if p 0 : 7 | InsertRecurison(list,q-1) 8 | Insert(list,q) 9 | 10 | def Insert(A,q): 11 | 12 | key = A[q+1] 13 | j=q 14 | while j>0 and A[j]>key: 15 | A[j+1]=A[j] 16 | j= j-1 17 | A[j+1]=key 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/2.3-5.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | __author__ = 'haoxiang' 3 | 4 | ''' 5 | 默认序列从小到大排序好了 6 | ''' 7 | def doubleSearch(A,n): 8 | 9 | i = 1 10 | j = len(A) 11 | while in: 17 | j = m-1 18 | else: 19 | i = m+1 20 | return 0 21 | test = [1,2,3,4,5,6,7,8,9] 22 | doubleSearch(test,6) -------------------------------------------------------------------------------- /src/2.3-7.py: -------------------------------------------------------------------------------- 1 | #-*- coding:utf-8 -*- 2 | __author__ = 'haoxiang' 3 | 4 | def bubble_sort(sort_list): 5 | iter_len = len(sort_list) 6 | if iter_len < 2: 7 | return sort_list 8 | for i in range(iter_len-1): 9 | for j in range(iter_len-i-1): 10 | if sort_list[j] > sort_list[j+1]: 11 | sort_list[j], sort_list[j+1] = sort_list[j+1], sort_list[j] 12 | return sort_list 13 | 14 | def find(S,x): 15 | bubble_sort(S) 16 | lens = len(S) 17 | i = 1 18 | j = lens-1 19 | while i x: 23 | j-=1 24 | if S[i]+S[j] == x: 25 | print(S[i],S[j]) 26 | i+=1 27 | j-=1 28 | 29 | 30 | test = [1,2,3,4,3,6,2,4,7,9,3,1,5,7,8,9] 31 | find(test,10) 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/4.1-2.py: -------------------------------------------------------------------------------- 1 | __author__ = 'haoxiang' 2 | 3 | # -*- coding: utf-8 -*- 4 | 5 | def findMaxSubarray(array,low,mid,high): 6 | 7 | lowSum = 0 8 | highSum = 0 9 | minLow = -9999 10 | minHigh = -9999 11 | maxMid = 0 12 | maxHigh = 0 13 | for i in range(mid,low,-1): 14 | lowSum += array[i] 15 | if lowSum>minLow: 16 | minLow = lowSum 17 | maxMid = i 18 | for j in range(mid,high): 19 | highSum += array[j] 20 | if highSum > minHigh: 21 | minHigh = highSum 22 | maxHigh = j 23 | print str(maxMid)+"---"+str(maxHigh) 24 | 25 | Array = [12,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7] 26 | print len(Array) 27 | findMaxSubarray(Array,0,len(Array)/2,len(Array)-1) -------------------------------------------------------------------------------- /src/6.2-2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | __author__ = 'haoxiang' 3 | 4 | 5 | def minHeapify(A,i): 6 | smallest = i 7 | left = 2*i 8 | right = 2*i+1 9 | if left<= i and A[left]= left and A[left] > A[largest]: 24 | largest = left 25 | if i >= right and A[right] > A[largest]: 26 | largest = right 27 | if largest != i: 28 | temp = A[i] 29 | A[i] = A[largest] 30 | A[largest] = temp 31 | maxHeapify(A,largest) 32 | print(A) 33 | test = [1,3,6,3,5,7,9,4,2,5,3,6,4,] 34 | maxHeapify(test,8) 35 | # minHeapify(test,4) -------------------------------------------------------------------------------- /src/6.3-1.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | __author__ = 'haoxiang' 3 | 4 | def minHeapify(A,length,i): 5 | smallest = i 6 | left = 2*i 7 | right = 2*i+1 8 | if left<= i and A[left] left and A[left] > A[i]: 24 | largest = left 25 | else : 26 | largest = i 27 | if length > right and A[right] > A[largest]: 28 | largest = right 29 | if largest != i: 30 | A[i],A[largest] = A[largest],A[i] 31 | maxHeapify(A,largest,length) 32 | return A 33 | 34 | def buildMinHeap(A): 35 | size = len(A) 36 | for i in range(size/2,1,-1): 37 | minHeapify(A,i) 38 | return A 39 | def buildMaxHeap(A): 40 | n = len(A) 41 | first = int(n/2-1) #最后一个非叶子节点 42 | for start in range(first,-1,-1): 43 | maxHeapify(A,start,n) 44 | return A 45 | 46 | def heapSort(A): 47 | buildMaxHeap(A) 48 | lens = len(A) 49 | for i in range(lens-1,-1,-1): 50 | A[i],A[0] = A[0],A[i] 51 | maxHeapify(A,0,i) 52 | print A 53 | 54 | 55 | 56 | def heap_sort(ary) : 57 | n = len(ary) 58 | first = int(n/2-1) #最后一个非叶子节点 59 | for start in range(first,-1,-1) : #构造大根堆 60 | max_heapify(ary,start,n-1) 61 | for end in range(n-1,0,-1): #堆排,将大根堆转换成有序数组 62 | ary[end],ary[0] = ary[0],ary[end] 63 | max_heapify(ary,0,end-1) 64 | print ary 65 | 66 | #最大堆调整:将堆的末端子节点作调整,使得子节点永远小于父节点 67 | #start为当前需要调整最大堆的位置,end为调整边界 68 | def max_heapify(ary,start,end): 69 | root = start 70 | while True : 71 | child = root*2 +1 #调整节点的子节点 72 | if child > end : break 73 | if child+1 <= end and ary[child] < ary[child+1] : 74 | child = child+1 #取较大的子节点 75 | if ary[root] < ary[child] : #较大的子节点成为父节点 76 | ary[root],ary[child] = ary[child],ary[root] #交换 77 | root = child 78 | else : 79 | break 80 | test = [1,3,2,4,8,5,9,36] 81 | # buildMinHeap(test) 82 | # buildMaxHeap(test) 83 | # heapSort(test) 84 | heapSort(test) -------------------------------------------------------------------------------- /src/6.5-7.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | __author__ = 'haoxiang' 3 | 4 | class Stack(): 5 | def __init__(self): 6 | return -------------------------------------------------------------------------------- /src/6.5.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | __author__ = 'haoxiang' 3 | 4 | def maxHeapify(A,i,length): 5 | largest = -1 6 | left = 2*i 7 | right = 2*i+1 8 | if length > left and A[left] > A[i]: 9 | largest = left 10 | else : 11 | largest = i 12 | if length > right and A[right] > A[largest]: 13 | largest = right 14 | if largest != i: 15 | A[i],A[largest] = A[largest],A[i] 16 | maxHeapify(A,largest,length) 17 | return A 18 | 19 | def heapMaximun(A): 20 | return A[0] 21 | 22 | def heapExtractMax(A): 23 | if len(A) < 1: 24 | return 25 | max = A[0] 26 | A[0] = A[len(A)-1] 27 | lens = len(A)-1 28 | maxHeapify(A,0,lens) 29 | print max 30 | def heapIncreaseKey(A,i,key): 31 | if key < A[i]: 32 | print 'new key is smaller' 33 | A[i] = key 34 | while i > 1 and A[i/2] = <=可以变化排序的升降 13 | if A[j] >= x: 14 | i +=1 15 | A[i],A[j] = A[j],A[i] 16 | A[i+1],A[r] = A[r],A[i+1] 17 | 18 | if i==r: 19 | return (p+r)/2 20 | else: 21 | return i+1 22 | 23 | 24 | def quickSort(A,p,r): 25 | if p= key: 39 | high -= 1 40 | while low < high and array[high] < key: 41 | array[low] = array[high] 42 | low += 1 43 | array[high] = array[low] 44 | array[low] = key 45 | return low 46 | 47 | 48 | def quick_sort(array,low,high): 49 | if low < high: 50 | key_index = sub_sort(array,low,high) 51 | quick_sort(array,low,key_index) 52 | quick_sort(array,key_index+1,high) 53 | 54 | 55 | ''' 56 | 测试 57 | ''' 58 | test = [9,2,5,3,8,8,5,3,6,8,4,19,3] 59 | #注意参数,第二个一定要从0开始 60 | quickSort(test,0,len(test)-1) 61 | # quick_sort(test,0,len(test)-1) 62 | # print(test) -------------------------------------------------------------------------------- /src/8.1.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | __author__ = 'haoxiang' 3 | 4 | def countSort(A,B,k): 5 | C=[] 6 | for i in range(0,k): 7 | C.append(0) 8 | for j in range(0,len(A)): 9 | C[A[j]] = C[A[j]] + 1 10 | for k in range(1,k): 11 | C[i] = C[i] + C[i-1] 12 | for l in range(len(A),0,-1): 13 | B[C[A[j]]] = A[j] 14 | C[A[j]] = C[A[j]] - 1 15 | print(B) 16 | 17 | test = [1,3,2,5,7] 18 | B = [] 19 | countSort(test,B,100) -------------------------------------------------------------------------------- /src/test.py: -------------------------------------------------------------------------------- 1 | # -*- coding :utf-8 -*- 2 | __author__ = 'haoxiang' 3 | # def max_heap(A, i, heapsize): 4 | # largest = -1 5 | # while True: 6 | # left = 2*i+1 7 | # right = 2*i + 2 8 | # if left 1) and self.com(self.HeapAry[i/2].key, self.HeapAry[i].key): 56 | self.HeapAry[i/2] ,self.HeapAry[i] = self.HeapAry[i] ,self.HeapAry[i/2] 57 | i = i/2 58 | 59 | def __MakeHeapify(self, i): 60 | if i > self.QueueLen()/2: 61 | return 62 | max_idx = i 63 | # find out the maximun(or minmun, judged by self.com method) one in 64 | # parent,left and right node. Identify it be max_idx 65 | if self.com(self.HeapAry[max_idx].key, self.HeapAry[i*2].key): 66 | max_idx = i*2 67 | if i*2+1 <= self.QueueLen() and self.com(self.HeapAry[max_idx].key, self.HeapAry[i*2 + 1].key): 68 | max_idx = i*2 + 1 69 | # if the max_idx is not parent, exchange parent and max_idx element. 70 | if max_idx != i: 71 | self.HeapAry[max_idx] ,self.HeapAry[i] = self.HeapAry[i] ,self.HeapAry[max_idx] 72 | self.__MakeHeapify(i*2) 73 | 74 | def DeQueue(self): 75 | head = self.HeapAry[1] 76 | last = self.HeapAry.pop() 77 | if (self.QueueLen() >= 1): 78 | self.HeapAry[1] = last 79 | self.__MakeHeapify(1) 80 | return head.obj 81 | 82 | def QueueLen(self): 83 | return len(self.HeapAry) - 1 84 | def Empty(self): 85 | return self.QueueLen() == 0 86 | 87 | class MaxPrioQueue(QHeap): 88 | """ 89 | Maximun priority queue. 90 | """ 91 | def __init__(self): 92 | # max queue use x < y to judge the change node configration. 93 | self.com = lambda x, y: x < y 94 | self.HeapAry = [] 95 | 96 | class MinPrioQueue(QHeap): 97 | """ 98 | Minmun priority queue. 99 | """ 100 | def __init__(self): 101 | # max queue use x > y to judge the change node configration. 102 | self.com = lambda x, y: x > y 103 | self.HeapAry = [] 104 | 105 | #----------------------------------------------------------------- 106 | # for test only. 107 | 108 | if __name__ == '__main__': 109 | h = MaxPrioQueue() 110 | chars = ['L', 'i', 'Y', 'i', 'W', 'e', 'n'] 111 | keys = [ 8 , 4 , 6 , 3 , 10, 9 , 5 ] 112 | for i in range(0, len(chars)): 113 | h.EnQueue(chars[i], keys[i]) 114 | result = [] 115 | while not h.Empty(): 116 | result.append(h.DeQueue()) 117 | print "length of result is %d:" % len(result) 118 | print "".join(result) 119 | 120 | h = MinPrioQueue() 121 | for i in range(0, len(chars)): 122 | h.EnQueue(chars[i], keys[i]) 123 | result = [] 124 | while not h.Empty(): 125 | result.append(h.DeQueue()) 126 | print "length of result is %d:" % len(result) 127 | print "".join(result) --------------------------------------------------------------------------------