所谓的BFPTR算法就是从n个数中寻找最小的K个数,主要思想可以参考注释,写得不是很好,特别是寻找中位数的中位数的时候,欢迎指正:
##BFPTR排序
##采用任意排序算法,将分组后的数据进行排序
#
__author__ = 'liu'
#coding = utf-8
'''
BFPTR排序
采用任意排序算法,将分组后的数据进行排序
这里采用插入排序算法,比如10个数,则low = 0, high = 10
'''
def insertsort(a, low, high):
for i in range(low + 1, high):
j = i
while j > 0 and a[j] < a[j - 1]:
a[j], a[j - 1] = a[j - 1], a[j]
j -= 1
'''
分治法,可参考快速排序,将快速排序里的key值由a[low]变为指定的索引值a[keyIdx]
为了得到将原始数据分为两部分的值对应的索引
'''
def Partion(a, low, high, keyIdx):
a[low],a[keyIdx] = a[keyIdx],a[low]
i = low
j = high
key = a[low]
while i < j:
while a[j] >= key and i < j:
j -= 1
if i < j:
a[i] = a[j]
i += 1
while a[i] <= key and i < j: