python笔记(谈谈算法)

  1. 二分法

    def bin_search_rec(data_set, value, high, low):
    """
    二分查找
    :param data_set: 列表
    :param value: 要查找的值
    :param low: 最低位
    :param high: 最高位
    :return:
    """
    if low < high:
    	mid = (high - low) // 2
    	if data_set[mid] > value:
    		bin_search_rec(data_set, value, mid - 1, low)
    	elif:data_set[mid] < value:
    		bin_search_rec(data_set, value, high, mid + 1)
    	else:
    		return mid
    else:
    	return None
    
    
    print(bin_search_rec(range(1000), 787, 0, 1000 - 1))
    	
    
  2. 冒泡排序版本一

    def bubble_sort(lis):
        """
        冒泡排序版本一,最小时间复杂度O(n^2)
        :param lis: 零散列表
        :return:
        """
    	lis_length = len(lis)
    	for i in range(lis_length - 1):
    		for j in range(lis_length - i - 1):
    			if lis[j] > lis[j + 1]:
    				lis[j], lis[j + 1] = lis[j + 1], lis[j]
    	return lis
    
  3. 冒泡排序版本二

    def bubble_sort(lis):
        """
        冒泡排序版本二,最小时间复杂度O(n)
        :param lis: 零散列表
        :return:
        """
    	lis_length = len(lis)
    	for i in range(lis_length - 1):
    		exchange = False     // 判断有没有做交换
    		for j in range(lis_length - i - 1):
    			if lis[j] > lis[j + 1]:
    				lis[j], lis[j + 1] = lis[j + 1], lis[j]
    				exchange = True
    		if not exchange:
    			return
    	return lis
    
  4. 选择排序

    def select_sort(lis):
    """
    选择排序,时间复杂度O(n^2)
    :param lis: 零散列表
    :return:
    """
    	list_lenth = len(lis)
    	for i in range(lis_length - 1):
    		min_loc = i
    		for j in range(i + 1, lis_length)):
    			if lis[min_loc] > lis[j]:
    				min_loc = j
    		lis[min_loc], lis[i] = lis[i], lis[min_loc]
    
  5. 插入排序

    def insert_sort(lis):
    	"""
        插入排序,时间复杂度O(n^2)
        :param lis: 零散列表
        :return:
        """
    	for i in range(1, len(lis)):
    		j = i - 1
    		temp = lis[i]
    		while j >= 0 and temp < lis[j]:
    			lis[j] = lis[j + 1]
    			j -= 1
    		lis[j + 1] = temp
    
  6. 快排

    def partion(lis, left, right):
        """
        时间复杂度O(n)
        先找出最左边的值,保证右边比它大,左边比他小
        :param lis: 零散列表
        :param left: 左边比那个值小的区域
        :param right: 右边比那个值大的区域
        :return:
        """
        import random
        ran = random.randint(left, right)
        lis[ran], lis[left] = lis[left], lis[ran]
        temp = lis[left]
        while left < right:
            while left < right and lis[right] >= temp:
                right -= 1
            lis[left] = lis[right]
            while left < right and lis[left] <= temp:
                left += 1
            lis[right] = lis[left]
        lis[right] = temp
        return left
    
    
    def quick_sort(lis, left, right):
        """
        快排时间复杂度O(nlogn),最坏情况O(n^2)
        进行划分,不断递归
        :param lis:
        :param left:
        :param right:
        :return:
        """
        if left < right:
            mid = partion(lis, left, right)
            quick_sort(lis, left, mid - 1)
            quick_sort(lis, mid + 1, right)
    

    内置函数

    import heapq
    heapq.heapify(x)
    heapq.heappop()
    heapq.heappush()
    
  7. 堆排序

    顺序存储
    python笔记(谈谈算法)_第1张图片

    父亲找孩子:左孩子:2 * i + 1 右孩子:2 * i + 2
    孩子找父亲: (j - 1) // 2

    python笔记(谈谈算法)_第2张图片
    python笔记(谈谈算法)_第3张图片

    def sift(lis, low, high):
        """
        堆排序
        :param lis:
        :param low:
        :param high:
        :return:
        """
        temp = lis[low]
        i = low
        j = 2 * i + 1
        while j <= high:
            if j < high and lis[j] < lis[j + 1]:
                j += 1
            if temp < lis[j]:
                lis[i] = lis[j]
                i = j
                j = 2 * i + 1
            else:
                break
        lis[i] = temp
    
    
    def heap_sort(lis):
        """
        堆排序
        :param lis:
        :return:
        """
        n = len(lis)
        # 建堆
        for i in range(n//2 - 1, -1, -1):
            sift(lis, i, n - 1)
        # 出数
        for i in range(n - 1, -1, -1):
            lis[0], lis[i] = lis[i], lis[0]
            sift(lis, 0, i - 1)
    
  8. 归并排序

    def merge(lis, low, mid, high):
        """
        归并排序
        例:2 5 7 8 9 1 3 4 6
        :param lis:
        :param low:
        :param mid:
        :param high:
        :return:
        """
        i = low
        j = mid + 1
        temp = []
        while i <= mid and j <= high:
            if lis[i] < lis[j]:
                temp.append(lis[i])
                i += 1
            else:
                temp.append(lis[j])
                j += 1
        while i <= mid:
            temp.append(lis[i])
            i += 1
        while j <= high:
            temp.append(lis[j])
            j += 1
        for i in range(low, high + 1):
            lis[i] = temp[i - low]
    
    
    def merge_sort(lis, low, high):
        """
        归并排序
        :param lis:
        :param low:
        :param high:
        :return:
        """
        if low < high:
            mid = (low + high) // 2
            merge_sort(lis, low, mid)
            merge_sort(lis, mid + 1, high)
            merge(lis, low, mid, high)
    
    lis = [2, 5, 7, 8, 9, 1, 3, 4, 6]
    merge_sort(lis, 0, len(lis) - 1)
    

你可能感兴趣的:(随笔)