Python程序员面试算法宝典---解题总结: 第4章 数组 4.6 寻找数组中第k小的元素

# -*- encoding: utf-8 -*-

'''
Python程序员面试算法宝典---解题总结: 第4章 数组 4.6 寻找数组中第小大的元素

题目:
寻找数组中第k小的元素

关键:
1 主函数中low = high必须保留
    # 如果恰好 index是low = high的时候并且是第k小的元素,需要特殊处理
    # 如果去除=号, [1, 1, 0, 0, 1, 0] 这个数组就无法得到正确结果
    if low <= high:
        index = partition(array, low, high)
        if index == k - 1:
            return array[index]
        elif index > k - 1:
            return findKthMin(array, low, index - 1, k)
        else:
            return findKthMin(array, index + 1, high, k)

2 划分函数中的主要逻辑
从后向前,找到小于枢轴的下标high
令前面的元素array[low] = array[high]
从前向后,找到大于枢轴的下标low
令后面的元素array[high] = array[low]
这样就将后面小于枢轴的元素和前面大于枢轴的元素进行了交换

参考:
Python程序员面试算法宝典
'''

def partition(array, low, high):
    if not array or low > high:
        return
    value = array[low]
    while low < high:
        while low < high and array[high] >= value:
            high -= 1
        array[low] = array[high]
        while low < high and array[low] <= value:
            low += 1
        array[high] = array[low]
    array[low] = value
    return low


def findKthMin(array, low, high, k):
    if not array:
        return
    if k < 0:
        return
    if k > len(array):
        print "can not get kth min number because array length is: {size}, k is: {k}".format(
            size=len(array),
            k=k
        )
        return
    # 如果恰好 index是low = high的时候并且是第k小的元素,需要特殊处理
    # 如果去除=号, [1, 1, 0, 0, 1, 0] 这个数组就无法得到正确结果
    if low <= high:
        index = partition(array, low, high)
        if index == k - 1:
            return array[index]
        elif index > k - 1:
            return findKthMin(array, low, index - 1, k)
        else:
            return findKthMin(array, index + 1, high, k)


def quickSort(array, low, high):
    if not array:
        return
    if low < high:
        index = partition(array, low, high)
        quickSort(array, low, index - 1)
        quickSort(array, index + 1, high)


def process():
    array = [1, 1, 0, 0, 1, 0]
    k = 3
    result = findKthMin(array, 0, len(array) - 1, k)
    print result
    array = [1, 5, 0, 3, 1, 6]
    k = 3
    result = findKthMin(array, 0, len(array) - 1, k)
    print result
    # quickSort(array, 0, len(array) - 1)
    # print array
    array = [1 for i in range(1, 7)]
    k = 3
    result = findKthMin(array, 0, len(array) - 1, k)
    print result
    array = [3]
    k = 1
    result = findKthMin(array, 0, len(array) - 1, k)
    print result
    array = [3]
    k = 2
    result = findKthMin(array, 0, len(array) - 1, k)
    print result



if __name__ == "__main__":
    process()









def validQuickSort():
    array = [1, 5, 0, 3, 1, 6]
    quickSort(array, 0, len(array) - 1)
    print array
    array = [1 for i in range(1, 7)]
    quickSort(array, 0, len(array) - 1)
    print array
    array = [1, 1, 0, 0, 1, 0]
    quickSort(array, 0, len(array) - 1)
    print array
    array = [1]
    quickSort(array, 0, len(array) - 1)
    print array


'''
[1, 1, 0, 0, 1, 0]
'''
def find2(array, low, high, k):
    if not array:
        return
    if low < high:
        index = partition(array, low, high)
        if index == k - 1:
            return array[index]
        elif index > k - 1:
            return find2(array, low, index - 1, k)
        else:
            return find2(array, index + 1, high, k)
    # 如果恰好 index是low = high的时候并且是第k小的元素,需要特殊处理
    elif low == high:
        if low == k - 1:
            return array[low]

 

你可能感兴趣的:(Python程序员面试算法宝典)