码迷,mamicode.com
首页 > 编程语言 > 详细

常见算法汇总

时间:2018-10-03 21:42:37      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:find   sorted   pop   while   arc   sele   ESS   汇总   dex   

def binary_search(arr, item):
    low = 0
    high = len(arr) - 1
    while low <= high:
        mid = (low + high) // 2
        guess = arr[mid]
        if item == guess:
            return mid
        if item < guess:
            high = mid - 1
        else:
            low = mid + 1
    return None

selection sort

def find_smallest(arr):
    smaller = arr[0]
    smaller_index = 0
    for i in range(1, len(arr)):
        if arr[i] < smaller:
            smaller = arr[i]
            smaller_index = i
    return smaller_index

def selection_sort(arr):
    sorted_arr = []
    while arr:
        smaller_index = find_smallest(arr)
        sorted_arr.append(arr.pop(smaller_index))
    return sorted_arr

常见算法汇总

标签:find   sorted   pop   while   arc   sele   ESS   汇总   dex   

原文地址:https://www.cnblogs.com/jeffrey-yang/p/9738875.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!