平时拿着上班当幌子 混混沌沌混日子 出了紧急状况才临时抱佛脚
有句话说的好 机会永远是留给那些时刻准备好的人
感叹时光飞逝 感叹岁月催人 没办法 硬着头皮上吧 与70多岁的两位老人比起来 我还属于青少年
准备吧 少年
几年前 这些基本算法拿笔就能写的 现在半天蹦不出几个字母 补基本功吧
记录今天的两个算法 只要理解了思想 实现起来 不算太难 --
def Quick_sort(A,start, end):
if start>=end:
return -1
pivot = A[start]
i = start
j = end
while i<j:
while (j>i and A[j]>=pivot):
j=j-1
if (j>i):
A[i]=A[j]
i=i+1
while(i<j and A[i]<=pivot):
i=i+1
if i<j:
A[j]=A[i]
j=j-1
A[i]=pivot
Quick_sort(A,start,i)
Quick_sort(A,i+1,end)
def Heap_sort(A):
need_continue = True
iteration = 0
while need_continue:
iteration = iteration + 1
print ("Iteration: %s" %iteration)
need_continue = False
for i in range(len(A)):
son_index = 2 * i + 1
if son_index < len(A):
max_child_index = son_index
if son_index + 1 < len(A) and A[son_index + 1] > A[son_index]:
max_child_index = son_index + 1
if A[i] < A[max_child_index]:
temp = A[i]
A[i] = A[max_child_index]
A[max_child_index] = temp
need_continue = True
else:
break
A = [3,78,67,47,89,65,89,37,48,87,22,85]
print(A)
Quick_sort(A,0,len(A)-1)
print("THe result by Quick sort : %s" %A)
A = [3,78,67,47,89,65,89,37,48,87,22,85]
Heap_sort(A)
print("The result by Heap sort : %s" %A)