算法题:

1.冒泡排序

# 需求:两个列表合并,并排序
a = [1, 6, 3, 8, 5]
b = [2, 3, 9, 4]
c = a + b

def mysorted(aList):
    """
    冒泡排序
    :param c:
    :return:
    """
    n=len(aList)
    for i in range(n):
        for j in range(n-i-1):
            if aList[j]>aList[j+1]:
                aList[j],aList[j+1]=aList[j+1],aList[j]

    return aList

if __name__ == '__main__':
    result=mysorted(c)
    print(result)

 

2.快速排序

# 快速排序
def fast_sort(aList, start, end):
    if start > end:
        return

    lowIndex = start
    midTarget = aList[start]
    hightIndex = end

    while lowIndex < hightIndex:
        while lowIndex < hightIndex and aList[hightIndex] >= midTarget:
            hightIndex -= 1
        aList[lowIndex] = aList[hightIndex]

        while lowIndex < hightIndex and aList[lowIndex] < midTarget:
            lowIndex += 1
        aList[hightIndex] = aList[lowIndex]

    aList[lowIndex] = midTarget
    fast_sort(aList, start, lowIndex - 1)
    fast_sort(aList, lowIndex + 1, end)


if __name__ == '__main__':
    a = [1, 3, 8, 5, 7]
    n = len(a)
    fast_sort(a, 0, n - 1)
    print(a)

 

3.求斐波那契数列即著名的兔子数列:1、1、2、3、5、8、13、21、34

def func(n):
    """
    斐波那契数列即著名的兔子数列:112358132134、……
    :param n: 
    :return: 
    """
    if n == 1 or n == 2:
        return 1
    else:
        return func(n - 1) + func(n - 2)


if __name__ == '__main__':
    result = func(4)
    print(result)