測開面試 | Python常問算法


1、排序

  • 從小到大排序:sorted(list)
  • 從大到小排序:sorted(list, reverse=True)
  • sort() 方法,改變原有數組的順序
  • sort(reverse=True)
#!/bin/Python
alist = [1, 4, 2, 3, 7, 6]
print(sorted(alist))
print(sorted(alist, reverse=True))
alist.sort()
print(alist)
alist.sort(reverse=True)
print(alist)

2、冒泡

  • 1.比較相鄰的元素,如果第一個比第二個大,就交換
  • 2.一輪遍歷,每兩個相鄰的元素,重復第 1 步,最大的放隊尾
  • 3.不包括已經排隊尾的,重復第 2 步
#!/bin/Python
# -*- coding: UTF-8 -*-
#冒泡排序
def bubble_sort(lists):
    #獲取數組長度
    count = len(lists) - 1
    #N個元素遍歷N次
    for index in range(count, 0, -1):
        #第i個和i+1個依次對比
        for sub_index in range(index):
            #大的元素冒上去
            if lists[sub_index] > lists[sub_index + 1]:
                lists[sub_index], lists[sub_index + 1] = lists[sub_index + 1], lists[sub_index]
    return lists
alist = [1, 4, 2, 3, 7, 6]
print(bubble_sort(alist))

3、快排

  • 1.從列表中挑出一個元素,作為基准值 key
  • 2.所有小於 key 的元素放左邊,所有大於 key 的元素放右邊
  • 3.分別遞歸左側列表,右側列表
#!/bin/Python
# -*- coding: UTF-8 -*-

#快速排序
def quick_sort(lists, left, right):
    #遞歸過程中,發現left和right一致時,停止遞歸,直接返回列表
    if left >= right:
        return lists
    #定義游標
    low = left
    high = right

    #取參考標志,最左邊的元素
    key = lists[low]
    while low < high:
        #從最右側向左,依次和標志元素對比,如果右側的元素大於等於標志元素
        while low < high and lists[high] >= key:
            #右側減1
            high -= 1
        #如果右側的元素小於標志元素,則low賦high值
        lists[low] = lists[high]

        #從最左側向右,依次和標志元素對比,如果左側的元素小於等於標志元素
        while low < high and lists[low] <= key:
            #左側加1
            low += 1
        #如果左側的元素大於標志元素,則high賦low值
        lists[high] = lists[low]

    #最后給high位置賦值
    lists[high] = key

    #處理左側元素
    quick_sort(lists, left, low - 1)
    #處理右側元素
    quick_sort(lists, low + 1, right)
    return lists

alist = [0, 10, 88, 19, 9, 1, 7]
print(quick_sort(alist, 0, 6))

4、堆排序

  • 堆排序指利用堆的數據結構設計的一種排序算法
  • 堆近似於一個完全二叉樹結構
  • 子節點的鍵值小於(或者大於)它的父節點
#!/bin/Python
# -*- coding: UTF-8 -*-

#堆排序
def heap_sort(lst):
    def sift_down(start, end):
        """最大堆調整"""
        root = start
        print "root %d start %d end %d" % (root, start, end)
        while True:
            child = 2 * root + 1
            #print "child index: %d" % child

            #終止條件,孩子的索引值超過數組最大長度
            if child > end:
                break
            #print "lst child value: %d" % lst[child]

            #確定最大的孩子節點的索引值
            if child + 1 <= end and lst[child] < lst[child + 1]:
                child += 1
                #print "child+1 index: %d" % child

            #孩子節點最大值和根節點交換
            if lst[root] < lst[child]:
                lst[root], lst[child] = lst[child], lst[root]
                #print "lstroot %d" %d lst[root], "lstchild %d" % lst[child]
                root = child
                #print "root %d" % root
            else:
                break

    print("---------------創建最大堆---------------")
    #創建最大堆
    print(xrange((len(lst) - 2) // 2, -1, -1))
    for start in xrange((len(lst) - 2) // 2, -1, -1):
        print "---->Loop start %d" % start
        sift_down(start, len(lst) - 1)
        print(lst)

    print("---------------排序過程---------------")
    #堆排序
    for end in xrange(len(lst) - 1, 0, -1):
        #首尾交換
        lst[0], lst[end] = lst[end], lst[0]
        #剩余重新堆排序
        sift_down(0, end - 1)
        print(lst)
    return lst


alist = [70, 60, 12, 40, 30, 8, 10]
print(heap_sort(alist))

5、二分查找

  • 二分查找又稱折半查找
  • 必須采用順序存儲結構
  • 必須按關鍵字大小有序排列
#!/bin/Python
# -*- coding: UTF-8 -*-

#二分查找
#原始數組
alist = [0, 1, 10, 88, 19, 9, 1]

def binary_search(arr, start, end, hkey):
    if start > end:
        #返回-1,表示程序出現異常
        return -1
    #先找到數組索引的中間值
    mid = start + (end - start) / 2
    #如果中間值大於查找的值,則從中間值左邊的數組中查找
    if arr[mid] > hkey:
        return binary_search(arr, start, mid - 1, hkey)
    #如果中間值小於查找的值,則從中間值右邊的數組中查找
    if arr[mid] < hkey:
        return binary_search(arr, mid + 1, end, hkey)
    #返回查找的值所在的索引值
    return mid

#給數組排序
alist = sorted(alist)
#打印出排序后的數組
print(alist)
#執行程序
print binary_search(alist, 0, 6, 9)

6、素數

  • 素數又稱質數
  • 0,1 不是素數
  • 除了 1 和它本身外,不能被其他自然數整除的數
#!/bin/Python
# -*- coding: UTF-8 -*-

#素數
def is_prime(n):
    #0,1 不是素數
    if n <= 1:
        return False

    #除了 1 和它本身外,不能被其他自然數整除的數
    for i in range(2, n):
        if n % i == 0:
            return False
    return True

for i in range(0, 100):
    if is_prime(i):
        print i

歡迎關注微信公眾號"測試開發Stack"


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM