zoukankan      html  css  js  c++  java
  • 面试题

    算法题:

    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)
  • 相关阅读:
    VMWare上的ubuntu系统安装VMWare Tools(图文)
    Ubuntu添加新分区
    emacs入门
    SQL UNION 操作符
    eclipse安装其他颜色主题包
    mysql左连接
    不能用notepad++编辑器编写python
    ImportError: No module named simplejson.scanner
    运行 python *.py 文件出错,如:python a.py
    doc命令大全(详细版)
  • 原文地址:https://www.cnblogs.com/weihu/p/11753133.html
Copyright © 2011-2022 走看看