【数据结构与算法】task3 排序&二分查找

排序

参考:https://github.com/wangzheng0822/algo/tree/master/python

归并排序

def merge_sort(a):
    _merge_sort_between(a, 0, len(a) - 1)

def _merge_sort_between(a, low, high):
    # The indices are inclusive for both low and high.
    if low < high:
        mid = low + (high - low) // 2
        _merge_sort_between(a, low, mid)
        _merge_sort_between(a, mid + 1, high)
        _merge(a, low, mid, high)

def _merge(a, low, mid, high):
    # a[low:mid], a[mid+1, high] are sorted.
    i, j = low, mid + 1
    tmp = []
    while i <= mid and j <= high:
        if a[i] <= a[j]:
            tmp.append(a[i])
            i += 1
        else:
            tmp.append(a[j])
            j += 1
    start = i if i <= mid else j
    end = mid if i <= mid else high
    tmp.extend(a[start:end + 1])
    a[low:high + 1] = tmp

if __name__ == "__main__":
    a1 = [3, 5, 6, 7, 8]
    a2 = [2, 2, 2, 2]
    a3 = [4, 3, 2, 1]
    a4 = [5, -1, 9, 3, 7, 8, 3, -2, 9]
    merge_sort(a1)
    print(a1)
    merge_sort(a2)
    print(a2)
    merge_sort(a3)
    print(a3)
    merge_sort(a4)
    print(a4)

快速排序

import random

def quick_sort(a):
    _quick_sort_between(a, 0, len(a) - 1)

def _quick_sort_between(a, low, high):
    if low < high:
        # get a random position as the pivot
        k = random.randint(low, high)
        a[low], a[k] = a[k], a[low]

        m = _partition(a, low, high)  # a[m] is in final position
        _quick_sort_between(a, low, m - 1)
        _quick_sort_between(a, m + 1, high)

def _partition(a, low, high):
    pivot, j = a[low], low
    for i in range(low + 1, high + 1):
        if a[i] <= pivot:
            j += 1
            a[j], a[i] = a[i], a[j]  # swap
    a[low], a[j] = a[j], a[low]
    return j

if __name__ == "__main__":
    a1 = [3, 5, 6, 7, 8]
    a2 = [2, 2, 2, 2]
    a3 = [4, 3, 2, 1]
    a4 = [5, -1, 9, 3, 7, 8, 3, -2, 9]
    quick_sort(a1)
    print(a1)
    quick_sort(a2)
    print(a2)
    quick_sort(a3)
    print(a3)
    quick_sort(a4)
    print(a4)

插入排序

def insertion_sort(a: List[int]):
    length = len(a)
    if length <= 1:
        return

    for i in range(1, length):
        value = a[i]
        j = i - 1
        while j >= 0 and a[j] > value:
            a[j + 1] = a[j]
            j -= 1
        a[j + 1] = value

if __name__ == "__main__":
    array = [5, 6, -1, 4, 2, 8, 10, 7, 6]
    insertion_sort(array)
    print(array)

冒泡排序

def bubble_sort(a):
    length = len(a)
    if length <= 1:
        return

    for i in range(length):
        made_swap = False
        for j in range(length - i - 1):
            if a[j] > a[j + 1]:
                a[j], a[j + 1] = a[j + 1], a[j]
                made_swap = True
        if not made_swap:
            break

if __name__ == "__main__":
    array = [5, 6, -1, 4, 2, 8, 10, 7, 6]
    bubble_sort(array)
    print(array)

选择排序

def selection_sort(a):
    length = len(a)
    if length <= 1:
        return

    for i in range(length):
        min_index = i
        min_val = a[i]
        for j in range(i, length):
            if a[j] < min_val:
                min_val = a[j]
                min_index = j
        a[i], a[min_index] = a[min_index], a[i]

if __name__ == "__main__":
    array = [5, 6, -1, 4, 2, 8, 10, 7, 6]
    selection_sort(array)
    print(array)

堆排序

 参考:https://www.baidu.com/link?url=swWPSFhb2Tcv40_9d2r024RXj9A9uyP56vjX6Wf05qjMNbPKQGC6kb51K8SZhdWIOLapT_m0Ht8m9OanVH2-h3WoE4ypbVOapf7ZRpSrUh_&wd=&eqid=b7e1f53b0013fbe1000000065cdf9f99

https://www.jianshu.com/p/d174f1862601

https://www.cnblogs.com/bingabcd/p/7425039.html

def HeapSort(input_list):
    
    #调整parent结点为大根堆
    def HeapAdjust(input_list,parent,length):
        
        temp = input_list[parent]
        child = 2*parent+1
        
        while child < length:
            if child+1 <length and input_list[child] < input_list[child+1]:
                child +=1
            
            if temp > input_list[child]:
                break
            input_list[parent] = input_list[child]
            parent = child
            child = 2*child+1
        input_list[parent] = temp
    
    if input_list == []:
        return []
    sorted_list = input_list
    length = len(sorted_list)
    #最后一个结点的下标为length//2-1
    #建立初始大根堆
    for i in range(0,length // 2 )[::-1]:
        HeapAdjust(sorted_list,i,length)
    
    for j in range(1,length)[::-1]:
        #把堆顶元素即第一大的元素与最后一个元素互换位置
        temp = sorted_list[j]
        sorted_list[j] = sorted_list[0]
        sorted_list[0] = temp
        #换完位置之后将剩余的元素重新调整成大根堆
        HeapAdjust(sorted_list,0,j)
        print('%dth' % (length - j))
        print(sorted_list)
    return sorted_list
    
        
if __name__ == '__main__':
    input_list = [50,123,543,187,49,30,0,2,11,100]
    print("input_list:")
    print(input_list)
    sorted_list = HeapSort(input_list)
    print("sorted_list:")
    print(input_list)

leetcode-239

参考:https://blog.csdn.net/weixin_41871126/article/details/85846113

class Solution(object):
    def maxSlidingWindow(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        if not nums:return []
        res,window = [],[]
        for i,j in enumerate(nums):
            if i >= k and window[0] <= i - k:
                window.pop(0)
            while window and nums[window[-1]] <= j:
                window.pop()
            window.append(i)
            if i >= k - 1:
                res.append(nums[window[0]])
        return res

实现 O(n) 时间复杂度内找到一组数据的第 K 大元素

参考:https://blog.csdn.net/u011519550/article/details/89417311

#给定一个无序列表,求出第K大的元素,要求复杂度O(n)
def find_k(test_list,k):
    flag=test_list[0]
    test_list.pop(0)
    l_list=[i for i in test_list if i < flag]
    r_list=[i for i in test_list if i >= flag]
    
    #结果递归的基线条件
    if len(r_list)==k-1:
        return flag
    elif len(r_list)>k-1:
        return find_k(r_list,k)
    else:
        #因为test_list.pop(0)让test_list少了一个元素,所以下面需要+1
        gap=len(test_list)-len(l_list)+1
        k=k-gap
        return find_k(l_list,k)
 
if __name__ == '__main__':
    test_list = [5, 4, 3, 2, 1,10,20,100]
    res=find_k(test_list,1)
    print(res)
 

二分查找

实现一个有序数组的二分查找算法

参考:https://blog.csdn.net/qq_20207459/article/details/79630382

print("有序数组中的二分查找")
key=int(input("请输入您要查找的整数:"))
c=[10,11,12,17,19,21,22,24,32,38,49,51,66,78,90]
def BinarySearch(key,c):
    lo,hi= 0,len(c)-1
    while lo<=hi:
        mid = int(lo+(hi-lo)/2)
        if key<c[mid]:
            hi = mid-1
        elif key>c[mid]:
            lo = mid+1
        else:
            return print("%s在数组中的索引为%s"%(key,mid))
    return print("%s不在该数组中"%key)
BinarySearch(key,c)

实现模糊二分查找算法(比如大于等于给定值的第一个元素)

参考:https://blog.csdn.net/m0_38019841/article/details/88235655#%E6%A8%A1%E7%B3%8A%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE%3A

#模糊二分[大于等于给定值的第一个元素的位置]
def fuzzyHalfSearch(nums,num):
    if not nums:
        return 
    if num>nums[-1]:
        return
    if num <= nums[0]:
        return 0
    elif num == nums[-1]:
        return len(nums)-1
    
    low = 0
    high = len(nums)-2
    while low <= high:
        medim = (high+low)/2
        if num>nums[medim] and num<=nums[medim+1]:
            return medim+1
        elif num > nums[medim+1]:
            low = medim+1
        elif num <= nums[medim]:
            high = medim
    return

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值