1. 程式人生 > >《演算法基礎》所有演算法的Python實現

《演算法基礎》所有演算法的Python實現

寫這篇文章並不是面向讀者的,主要就是當記筆記。

我的程式設計基礎太爛,我之前想我以後要搞工程,也不搞ACM,所以一直沒有重視演算法。但是現在想來,之前的想法就實在可笑了。演算法基礎不行,不論是搞工程還是搞學術,都太容易觸碰到自己的天花板。一句話:目光短淺!

現在打算每天晚上花1~2個小時用在演算法上。看演算法書或在網上刷題。

由於個人能力尚且太淺,如果真的有人看我下面的程式碼的話,還請多多指教!

為了比較效能,我寫了個用於函式計時的裝飾器:

import time
from functools import wraps

def ftimer(func):
    @wraps(func)
def function_timer(*args,**kwargs): t0=time.time() result=func(*args,**kwargs) t1=time.time() print 'the running time of function "%s" is %.12f seconds' %(func.__name__,t1-t0) return result return function_timer

於是每次寫程式碼前要先

from decorators import ftimer

P12 LINEAR-SEARCH(A,n,x)

@ftimer
def linear_search(A,n,x):
    ans='NOT_FOUND'
    for i in range(n):
        if A[i]==x:
            ans=i+1
    return ans

nums=[0,5,2,3,1,4,6]
n=len(nums)
x=int(raw_input('input a num to search:'))
ans=linear_search(nums,n,x)
print 'the anwser is',ans

P13 BETTER-LINEAR-SEARCH(A,n,x)

@ftimer
def linear_search(A,n,x):
    ans='NOT_FOUND'
    for i in range(n):
        if A[i]==x:
            ans=i+1
            break
    return ans

nums=[0,5,2,3,1,4,6]
n=len(nums)
x=int(raw_input('input a num to search:'))
ans=linear_search(nums,n,x)
print 'the anwser is',ans

P14 SENTINEL-LINEAR-SEARCH(A,n,x)

@ftimer
def linear_search(A,n,x):
    last=A[n-1]
    A[n-1]=x
    i=0
    while A[i]!=x:
        i+=1
    A[n-1]=last
    if i<n-1 or A[n-1]==x:
        return i+1
    return 'NOT_FOUND'

nums=[0,5,2,3,1,4,6]
n=len(nums)
x=int(raw_input('input a num to search:'))
ans=linear_search(nums,n,x)
print 'the anwser is',ans

今天到此為止。打卡,嘀~
2017/9/28

插入排序

def insert_sort(lists):
    # 插入排序
    count = len(lists)
    for i in range(1, count):
        key = lists[i]
        j = i - 1
        while j >= 0:
            if lists[j] > key:
                lists[j + 1] = lists[j]
                lists[j] = key
            j -= 1
    return lists

希爾排序
這個還需要好好研究一下

def shell_sort(lists):
    # 希爾排序
    count = len(lists)
    step = 2
    group = count / step
    while group > 0:
        for i in range(0, group):
            j = i + group
            while j < count:
                k = j - group
                key = lists[j]
                while k >= 0:
                    if lists[k] > key:
                        lists[k + group] = lists[k]
                        lists[k] = key
                    k -= group
                j += group
        group /= step
    return lists

氣泡排序

def bubble_sort(lists):
    # 氣泡排序
    count = len(lists)
    for i in range(0, count):
        for j in range(i + 1, count):
            if lists[i] > lists[j]:
                lists[i], lists[j] = lists[j], lists[i]
    return lists

快速排序
這個還需要再看看

def quick_sort(lists, left, right):
    # 快速排序
    if left >= right:
        return lists
    key = lists[left]
    low = left
    high = right
    while left < right:
        while left < right and lists[right] >= key:
            right -= 1
        lists[left] = lists[right]
        while left < right and lists[left] <= key:
            left += 1
        lists[right] = lists[left]
    lists[right] = key
    quick_sort(lists, low, left - 1)
    quick_sort(lists, left + 1, high)
    return lists

選擇排序

def select_sort(lists):
    # 選擇排序
    count = len(lists)
    for i in range(0, count):
        min = i
        for j in range(i + 1, count):
            if lists[min] > lists[j]:
                min = j
        lists[min], lists[i] = lists[i], lists[min]
    return lists

歸併排序

def merge(left, right):
    i, j = 0, 0
    result = []
    while i < len(left) and j < len(right):
        if left[i] <= right[j]:
            result.append(left[i])
            i += 1
        else:
            result.append(right[j])
            j += 1
    result += left[i:]
    result += right[j:]
    return result

def merge_sort(lists):
    # 歸併排序
    if len(lists) <= 1:
        return lists
    num = len(lists) / 2
    left = merge_sort(lists[:num])
    right = merge_sort(lists[num:])
    return merge(left, right)