首页 > 编程语言 > 详细

常见算法汇总

时间:2018-10-03 11:03:50      阅读:144      评论:0      收藏:0      [点我收藏+]
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

常见算法汇总

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!