排序

作者: reeuq | 来源:发表于2018-07-10 19:21 被阅读0次

    快排

    #quick sort
    def quickSort(L, low, high):
        i = low 
        j = high
        if i >= j:
            return L
        key = L[i]
        while i < j:
            while i < j and L[j] >= key:
                j = j-1                                                             
            L[i] = L[j]
            while i < j and L[i] <= key:    
                i = i+1 
            L[j] = L[i]
        L[i] = key 
        quickSort(L, low, i-1)
        quickSort(L, j+1, high)
        return L
    
    def quick_sort(data, start, end):
        if start >= end:
            return
        mid = data[start]
        small = start
        for i in range(start+1, end+1):
            if data[i] > mid:
                small += 1
                if small != i:
                    temp = data[small]
                    data[small] = data[i]
                    data[i] = temp
        data[start] = data[small]
        data[small] = mid
        quick_sort(data, start, small - 1)
        quick_sort(data, small + 1, end)
    

    冒泡

    def bubble_sort(nums):
        for i in range(len(nums) - 1):  # 这个循环负责设置冒泡排序进行的次数
            for j in range(len(nums) - i - 1):  # j为列表下标
                if nums[j] > nums[j + 1]:
                    nums[j], nums[j + 1] = nums[j + 1], nums[j]
        return nums
    
    print(bubble_sort([45, 32, 8, 33, 12, 22, 19, 97]))
    # 输出:[8, 12, 19, 22, 32, 33, 45, 97]
    

    相关文章

      网友评论

          本文标题:排序

          本文链接:https://www.haomeiwen.com/subject/nmgeuftx.html