zoukankan      html  css  js  c++  java
  • 算法

    # 搜索最小值
    def index_of_minimum(lyst):
        """Returns the index of the minimum item."""
        min_index = 0
        current_index = 0
    
        while current_index < len(lyst):
            if lyst[min_index] > lyst[current_index]:
                min_index = current_index
            current_index += 1
    
            return min_index
    # 二叉搜索
    def binary_search(target, sortedLyst):
        left = 0
        right = len(sortedLyst) -1
        while left < right:
            mid_point = (left + right) // 2
            if target == sortedLyst[mid_point]:
                return target
            elif target <= sortedLyst[mid_point]:
                right = mid_point - 1
            else:
                left = mid_point + 1
        return -1
    def swap(lyst, i, j):
        temp = lyst[i]
        lyst[i] = lyst[j]
        lyst[j] = temp
    
    
    # 选择排序
    def selection_sort(lyst):
        i = 0
        while i < len(lyst) - 1:
            min_index = i
            j = i + 1
            while j < len(lyst):
                if lyst[j] < lyst[min_index]:
                    min_index = j
                j += 1
            if min_index != i:
                swap(lyst, min_index, i)
    
            i += 1
    
    
    # 冒泡排序
    def bubble_sort(lyst):
        n = len(lyst)
        while n > 1:
            i = 1
            while i < n:
                if lyst[i] < lyst[i-1]:
                    swap(lyst, i, i-1)
                i += 1
            n -= 1
    
    
    # 插入排序
    def insertion_sort(lyst):
        i = 1
        while i < len(lyst):
            item_to_insert = lyst[i]
            j = i -1
            while j >= 0:
                if item_to_insert < lyst[j]:
                    lyst[j+1] = lyst[j]
                    j -= 1
    
                else:
                    break
            lyst[j+1] = item_to_insert
            i += 1
  • 相关阅读:
    mysql BETWEEN操作符 语法
    mysql IN操作符 语法
    mysql LIKE通配符 语法
    mysql TOP语句 语法
    mysql DELETE语句 语法
    mysql Update语句 语法
    mysql INSERT语句 语法
    mysql ORDER BY语句 语法
    mysql OR运算符 语法
    mysql AND运算符 语法
  • 原文地址:https://www.cnblogs.com/yugengde/p/8472212.html
Copyright © 2011-2022 走看看