leetcode刷题之排序算法python实现

参考文章

十大经典排序算法(动图演示)
在这里插入图片描述

  • 快速排序算法:
def quick_sort(nums, first, last):
    if first > last:
        return 
    mid_val = nums[first]
    high = last
    low = first
    while low < high:
        while low < high and nums[high] >= mid_val:
            high -= 1
        nums[low] = nums[high]
        while low < high and nums[low] < mid_val:
            low += 1
        nums[high] = nums[low]
        
    nums[low] = mid_val
    quick_sort(nums, first, low-1)
    quick_sort(nums, low+1, last)
if __name__ == "__main__":
    start = time.time()
#    for i in range(1000):
    a = [4,2,4,5,7,8,12,23,1]
    quick_sort(a, 0, len(a)-1)
    end = time.time()
    print(a)
    print('Running time: %s Seconds'%(end-start))
  • 归并排序
def merge_sort(nums):
    if len(nums) <= 1:
        return nums
    middle = len(nums) / 2
    left_num = nums[: middle]
    right_num = nums[middle:]
    return merge(merge_sort(left_num), merge_sort(right_num))

def merge(left_num, right_num):
    len_left = len(left_num)
    len_right = len(right_num)
    i = j = 0
    result = []
    while i < len_left and j < len_right:
        if left_num[i] > right_num[j]:
            result.append(right_num[j])
            j += 1
        else:
            result.append(left_num[i])
            i += 1
    if i == len_left and j < len_right:
        for m in right_num[j:]:
            result.append(m)
    if j == len_right and i < len_left:
        for m in left_num[i:]:
            result.append(m)
    return result


if __name__ == "__main__":
    start = time.time()
#    for i in range(1000):
    a = [4,2,4,5,7,8,12,23,1]
    result = mergesort(a)
    end = time.time()
    print(result)
    print('Running time: %s Seconds'%(end-start))
from collections import deque
def heap_adjust(nums, start, end):
    temp = nums[start]
    i = start
    j = start * 2
    
    while j <= end:
        if j < end and nums[j] < nums[j+1]:
            j += 1
        if temp < nums[j]:
            nums[i] = nums[j]
            i = j
            j = i * 2
        else:
            break
    nums[i] = temp
            


def swap(nums, i, j):
    nums[i], nums[j] = nums[j], nums[i]
    return nums


def heap_sort(nums):
    len_heap = len(nums) - 1
    len_mid_heap = int(len_heap / 2)
    
    for i in range(len_mid_heap):
        heap_adjust(nums, len_mid_heap-i, len_heap)
        
    for i in range(len_heap-1):
        swap(nums, len_heap-i, 1)
        heap_adjust(nums, 1, len_heap-i-1)
    return list(nums)[1:]
        
def main():
    L = deque([50, 16, 30, 10, 60,  90,  2, 80, 70])
    L.appendleft(0)
    print(heap_sort(L))


if __name__ == '__main__':
    main()   
        

方法二,更简洁一点

import random

def MAX_Heapify(heap,HeapSize,root):#在堆中做结构调整使得父节点的值大于子节点

    left = 2*root + 1
    right = left + 1
    larger = root
    if left < HeapSize and heap[larger] < heap[left]:
        larger = left
    if right < HeapSize and heap[larger] < heap[right]:
        larger = right
    if larger != root:#如果做了堆调整则larger的值等于左节点或者右节点的,这个时候做对调值操作
        heap[larger],heap[root] = heap[root],heap[larger]
        MAX_Heapify(heap, HeapSize, larger)

def Build_MAX_Heap(heap):#构造一个堆,将堆中所有数据重新排序
    HeapSize = len(heap)#将堆的长度当独拿出来方便
    for i in xrange((HeapSize -2)//2,-1,-1):#从后往前出数
        MAX_Heapify(heap,HeapSize,i)

def HeapSort(heap):#将根节点取出与最后一位做对调,对前面len-1个节点继续进行对调整过程。
    Build_MAX_Heap(heap)
    for i in range(len(heap)-1,-1,-1):
        heap[0],heap[i] = heap[i],heap[0]
        MAX_Heapify(heap, i, 0)
    return heap

if __name__ == '__main__':
    a = [30,50,57,77,62,78,94,80,84]
    print a
    HeapSort(a)
    print a
    b = [random.randint(1,1000) for i in range(1000)]
    print b
    HeapSort(b)
    print b

冒泡排序

选择排序

def insert_sort(nums):   
    for i in range(1, len(nums)):
        for j in range(i, 0, -1):
            if nums[j] < nums[j-1]:
                nums[j], nums[j-1] = nums[j-1], nums[j]
                
nums = [2,1,4,57,4,2,57,3,9]
insert_sort(nums)
print(nums)
            

插入排序

def insert_sort(nums):
    for i in range(1, len(nums)):
        pre_index = i -1 
        cur_val = nums[i]
        while pre_index and nums[pre_index] > cur_val:
            nums[pre_index+1] = nums[pre_index]
            pre_index -= 1
        nums[pre_index + 1] = cur_val
    print(nums)

希尔排序

def shell_sort(nums):
    gap = len(nums) // 2
    while gap >= 1:
        for i in range(gap, len(nums)-gap):
            pre_index = i
            cur_val = nums[i + gap]
            while pre_index >= gap and nums[pre_index] > cur_val:
                nums[pre_index+gap] = nums[pre_index]
                pre_index -= gap
            nums[pre_index + gap] = cur_val
        gap = gap // 2

    print(nums)
            

if __name__ == '__main__':
    nums = [1,3,2,4,6,1,112,23,34]
    shell_sort(nums)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值