• python代码小实践之data_structure_and_algorithm


    二分查找
    def bin_sort(l_head,l_index):
        length = len(l_head)
        for i in range(length-1):
            if i == 0:
                continue
            else:
                head = 0
                tail = i - 1
                temp_h = l_head[i]
                temp_i = l_index[i]
                while head <= tail:
                    mid = (head+tail)/2
                    if(l_head[mid]<temp_h):
                        head = mid + 1
                    else:
                        tail = mid - 1
                l_head[head+1:i+1] = l_head[head:i]
                l_index[head+1:i+1] = l_index[head:i]
                l_head[head] = temp_h
                l_index[head] = temp_i
    '''
    Created on 2012-7-5
     
    @author: mainred
    '''
    归并排序
    def merge(A, p, q, r):
        """merge sorted sequence A[p,...,q] and A[q+1,...,r]
        """
        l1 = A[p:q+1]
        l2 = A[q+1:r+1]
        i1 = i2 = 0
        i = p
        while i1<len(l1) and i2<len(l2):
            if l1[i1]<l2[i2]:
                A[i] = l1[i1]
                i1 += 1
            else:
                A[i] = l2[i2]
                i2 += 1
            i += 1
        if i1 == len(l1):
            A[i:r+1] = l2[i2:len(l2)]
        if i2 == len(l2):
            A[i:r+1] = l1[i1:len(l1)]
        return A
     
    def merge_sort(A, p, r):
        if p < r:
            m = (p+r)/2
            merge_sort(A, p, m)
            merge_sort(A, m+1, r)
            merge(A, p, m, r)
    插入排序
    '''
    Created on 2012-7-5
     
    @author: mainred
    '''
    def insert_sort(A):
        """Implement insert sort on A
        
        Args:
            A: A list to be sorted
        
        Returns:
        A list containing sorted parameters.
        For example: [1, 4, 5, 9]
        
        """
        if A is None or len(A)<3:
            return A
        l = len(A)
     
        for i in range(2,l):
            A[0] = A[i]
            n = i-1
            while(A[0]<A[n]):
                A[n+1] = A[n]
                n -= 1
            A[n+1] = A[0]    
        return A
  • 相关阅读:
    使用SELECT语句检索数据
    redis的安装和使用【2】redis的java操作
    Python之数据结构改造
    InnoDB undo log物理结构的初始化
    Redis-RDB持久化设置
    MySql(四)Select条件查询
    Node.js TLS/SSL
    Node.js 定时器
    Node.js 系统
    Node.js 字符串解码器
  • 原文地址:https://www.cnblogs.com/haoqingchuan/p/2629956.html
Copyright © 2020-2023  润新知