zoukankan      html  css  js  c++  java
  • python排序

    import random
    
    def bubble_sort(li):
        for i in range(len(li)-1):
            exch_flag = False
            for j in range(len(li)-i-1):
                if li[j+1] < li[j]:
                    li[j],li[j+1] = li[j+1],li[j]
                    exch_flag = True
            if not exch_flag:
                break
            
    def insert_sort(li):
        for i in range(1,len(li)):
            tmp = li[i]
            j = i-1
            while j>=0 and tmp < li[j]:
                li[j+1] = li[j]
                j -= 1
            li[j+1]=tmp
    def select_sort(li):
        for i in range(len(li)-1):
            min_loc = i
            for j in range(i+1,len(li)):
                print(i)
                if li[j] < li[min_loc]:
                    min_loc = j
            if min_loc != i:
                li[i],li[min_loc] = li[min_loc],li[i]
    
    def _partition(li,left,right):
        temp = li[left]
        while left < right:
            while left < right and temp < li[right]:
                right -= 1
            li[left] = li[right]
            while left < right and temp > li[left]:
                left += 1
            li[right] = li[left]
        li[left] = temp
        return left
    def _quick_sort(li,left,right):
        if left < right:
            mid = _partition(li,left,right)
            _quick_sort(li,left,mid)
            _quick_sort(li,mid+1,right)
    def quick_sort(li):
        left = 0
        right = len(li)-1
        _quick_sort(li,left,right)
    def sift(li,low,high):
        i = low
        j = i * 2 + 1
        tmp = li[i]
        while j <= high:
            if j < high and li[j] < li[j+1]:
                j += 1
            if tmp < li[j]:
                li[i] = li[j]
                i = j
                j = 2 * i + 1
            else:
                break
        li[i] = tmp
        
    def heap_sort(li):
        n = len(li)
        for i in range(n//2-1,-1,-1):
            sift(li,i,n-1)
        for j in range(n-1,-1,-1):
            li[0],li[j] = li[j],li[0]
            sift(li,0,j-1)
    def merge(li,left,mid,right):
        i = left
        j = mid+1
        ltmp = []
        while i<=mid and j<=right:
            if li[i] <= li[j]:
                ltmp.append(li[i])
                i += 1
            else:
                ltmp.append(li[j])
                j += 1
        while i<= mid:
            ltmp.append(li[i])
            i += 1
        while j <= right:
            ltmp.append(li[j])
            j+= 1
        li[left:right+1] = ltmp
    def _merge_sort(li,left,right):
        if left < right:
            mid = (left+right)//2
            _merge_sort(li,left,mid)
            _merge_sort(li,mid+1,right)
            merge(li,left,mid,right)
    def merge_sort(li):
        left = 0
        right = len(li)-1
        _merge_sort(li,left,right)
    def sort_test():
        li = []
        for i in range(100):
            li.append(i)
        random.shuffle(li)
        select_sort(li)
        print(li)
    sort_test()
  • 相关阅读:
    用Python计算最长公共子序列和最长公共子串
    两个有序数列,求中间值 Median of Two Sorted Arrays
    python判断平衡二叉树
    经验之谈:如何为你的机器学习问题选择合适的算法?
    跑道没了,它们还在跑:澳门赛狗业的遗孤
    钉钉:工作的归工作,生活的…也归工作?
    假·照骗,真·社交焦虑
    苹果市值破万亿,iPhone 会涨价吗?
    写日志(log)
    多维数组排序
  • 原文地址:https://www.cnblogs.com/Json28/p/11032030.html
Copyright © 2011-2022 走看看