def hanoi(n, a, b, c): if n>0: hanoi(n-1, a, c, b) print("moving from %s to %s" % (a, c)) hanoi(n-1, b, a, c) hanoi(3, 'A', 'B', 'C') """ moving from A to C moving from A to B moving from C to B moving from A to C moving from B to A moving from B to C moving from A to C """
def bin_search_rec(data_set, value, low, high): if low <= high: mid = (low + high) // 2 if data_set[mid] == value: return mid elif data_set[mid] > value: return bin_search_rec(data_set, value, low, mid - 1) else: return bin_search_rec(data_set, value, mid + 1, high) else: return
import random from .timewrap import * @cal_time def bubble_sort(li): for i in range(len(li) - 1): # i 表示趟數 # 第 i 趟時: 無序區:(0,len(li) - i) for j in range(0, len(li) - i - 1): if li[j] > li[j+1]: li[j], li[j+1] = li[j+1], li[j] #冒泡排序優化 #若是冒泡排序中執行一趟而沒有交換,則列表已是有序狀態,能夠直接結束算法。 @cal_time def bubble_sort_2(li): for i in range(len(li) - 1): # i 表示趟數 # 第 i 趟時: 無序區:(0,len(li) - i) change = False for j in range(0, len(li) - i - 1): if li[j] > li[j+1]: li[j], li[j+1] = li[j+1], li[j] change = True if not change: return li = list(range(10000)) # random.shuffle(li) # print(li) bubble_sort_2(li) print(li) ''' 列表每兩個相鄰的數,若是前邊的比後邊的大,那麼交換這兩個數 時間複雜度O(n2) '''
import random from .timewrap import * @cal_time def select_sort(li): for i in range(len(li) - 1): # i 表示趟數,也表示無序區開始的位置 min_loc = i # 最小數的位置 for j in range(i + 1, len(li) - 1): if li[j] < li[min_loc]: min_loc = j li[i], li[min_loc] = li[min_loc], li[i] li = list(range(10000)) random.shuffle(li) print(li) select_sort(li) print(li) ''' 一趟遍歷記錄最小的數,放到第一個位置; 再一趟遍歷記錄剩餘列表中最小的數,繼續放置; 時間複雜度:O(n2) '''
import random from .timewrap import * @cal_time def insert_sort(li): for i in range(1, len(li)): # i 表示無序區第一個數 tmp = li[i] # 摸到的牌 j = i - 1 # j 指向有序區最後位置 while li[j] > tmp and j >= 0: #循環終止條件: 1. li[j] <= tmp; 2. j == -1 li[j+1] = li[j] j -= 1 li[j+1] = tmp li = list(range(10000)) random.shuffle(li) print(li) insert_sort(li) print(li) ''' 列表被分爲有序區和無序區兩個部分。最初有序區只有一個元素。 每次從無序區選擇一個元素,插入到有序區的位置,直到無序區變空。 O(n2) '''
from .timewrap import * import random def _sift(li, low, high): """ :param li: :param low: 堆根節點的位置 :param high: 堆最有一個節點的位置 :return: """ i = low # 父親的位置 j = 2 * i + 1 # 孩子的位置 tmp = li[low] # 原省長 while j <= high: if j + 1 <= high and li[j + 1] > li[j]: # 若是右孩子存在而且右孩子更大 j += 1 if tmp < li[j]: # 若是原省長比孩子小 li[i] = li[j] # 把孩子向上移動一層 i = j j = 2 * i + 1 else: li[i] = tmp # 省長放到對應的位置上(幹部) break else: li[i] = tmp # 省長放到對應的位置上(村民/葉子節點) def sift(li, low, high): """ :param li: :param low: 堆根節點的位置 :param high: 堆最有一個節點的位置 :return: """ i = low # 父親的位置 j = 2 * i + 1 # 孩子的位置 tmp = li[low] # 原省長 while j <= high: if j + 1 <= high and li[j+1] > li[j]: # 若是右孩子存在而且右孩子更大 j += 1 if tmp < li[j]: # 若是原省長比孩子小 li[i] = li[j] # 把孩子向上移動一層 i = j j = 2 * i + 1 else: break li[i] = tmp @cal_time def heap_sort(li): n = len(li) # 1. 建堆 for i in range(n//2-1, -1, -1): sift(li, i, n-1) # 2. 挨個出數 for j in range(n-1, -1, -1): # j表示堆最後一個元素的位置 li[0], li[j] = li[j], li[0] # 堆的大小少了一個元素 (j-1) sift(li, 0, j-1) li = list(range(10000)) random.shuffle(li) heap_sort(li) print(li) # li=[2,9,7,8,5,0,1,6,4,3] # sift(li, 0, len(li)-1) # print(li) '''' 創建堆 獲得堆頂元素,爲最大元素 去掉堆頂,將堆最後一個元素放到堆頂,此時可經過一次調整從新使堆有序。 堆頂元素爲第二大元素。 重複步驟3,直到堆變空。 時間複雜度O(nlgn) '''
import heapq, random li = [5,8,7,6,1,4,9,3,2] heapq.heapify(li) print(heapq.heappop(li)) print(heapq.heappop(li)) def heap_sort(li): heapq.heapify(li) n = len(li) new_li = [] for i in range(n): new_li.append(heapq.heappop(li)) return new_li li = list(range(10000)) random.shuffle(li) # li = heap_sort(li) # print(li) print(heapq.nlargest(100, li))
import random from timewrap import * import copy import sys def merge(li, low, mid, high): i = low j = mid + 1 ltmp = [] while i <= mid and j <= high: if li[i] < li[j]: ltmp.append(li[i]) i += 1 else: ltmp.append(li[j]) j += 1 while i <= mid: ltmp.append(li[i]) i += 1 while j <= high: ltmp.append(li[j]) j += 1 li[low:high+1] = ltmp def _merge_sort(li, low, high): if low < high: # 至少兩個元素 mid = (low + high) // 2 _merge_sort(li, low, mid) _merge_sort(li, mid+1, high) merge(li, low, mid, high) print(li[low:high+1]) def merge_sort(li): return _merge_sort(li, 0, len(li)-1) li = list(range(16)) random.shuffle(li) print(li) merge_sort(li) print(li)
def insert_sort(li): for i in range(1, len(li)): # i 表示無序區第一個數 tmp = li[i] # 摸到的牌 j = i - 1 # j 指向有序區最後位置 while li[j] > tmp and j >= 0: #循環終止條件: 1. li[j] <= tmp; 2. j == -1 li[j+1] = li[j] j -= 1 li[j+1] = tmp def shell_sort(li): d = len(li) // 2 while d > 0: for i in range(d, len(li)): tmp = li[i] j = i - d while li[j] > tmp and j >= 0: li[j+d] = li[j] j -= d li[j+d] = tmp d = d >> 1
# 0 0 1 1 2 4 3 3 1 4 5 5 import random import copy from timewrap import * @cal_time def count_sort(li, max_num = 100): count = [0 for i in range(max_num+1)] for num in li: count[num]+=1 li.clear() for i, val in enumerate(count): for _ in range(val): li.append(i) @cal_time def sys_sort(li): li.sort() li = [random.randint(0,100) for i in range(100000)] li1 = copy.deepcopy(li) count_sort(li) sys_sort(li1)
import random from .timewrap import * def list_to_buckets(li, iteration): """ :param li: 列表 :param iteration: 裝桶是第幾回迭代 :return: """ buckets = [[] for _ in range(10)] for num in li: digit = (num // (10 ** iteration)) % 10 buckets[digit].append(num) return buckets def buckets_to_list(buckets): return [num for bucket in buckets for num in bucket] # li = [] # for bucket in buckets: # for num in bucket: # li.append(num) @cal_time def radix_sort(li): maxval = max(li) # 10000 it = 0 while 10 ** it <= maxval: li = buckets_to_list(list_to_buckets(li, it)) it += 1 return li li = [random.randint(0,1000) for _ in range(100000)] radix_sort(li)
def twoSum(nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ for i in range(len(nums)): for j in range(i+1,len(nums)): if nums[i]+nums[j] == target: return [i,j]
def twoSum(nums, target): d = {} for i in range(len(nums)): if target - nums[i] in d: return i, d[target-nums[i]] else: d[nums[i]] = i
def partition(array, left, right): well = left for i in range(left, right): if array[i] > array[right]: array[i], array[well] = array[well], array[i] well += 1 array[well], array[right] = array[right], array[well] return well def findk(l,low,high,k): if low<=high: mid=partition(l, low, high) if mid==len(l)-k: res=l[mid] return res elif mid>len(l)-k: return findk(l,low,mid-1,k) else: return findk(l,mid+1,high,k) l=[1,23,4,5,64,68,12,45,666,999,69] res=findk(l,0,len(l)-1,6) print("res:",res,l)
def kp(data, left, right): tem = data[left] while left < right: while left < right and data[right] >= tem: right -= 1 data[left] = data[right] while left < right and data[left] <= tem: left += 1 data[right] = data[left] data[right] = tem return left def qk(data, left, right): if left <right: mid = kp(data, left,right) qk(data,left, mid-1) qk(data,mid+1, right) k=5 data = [1, 23, 4, 5, 64, 68, 12, 45, 666, 999, 69] qk(data, 0, len(data)-1) data.reverse() print(data) print(data[len(data) -1-k-1])