快排、堆排和归并排序的Python实现

24 篇文章 0 订阅
15 篇文章 0 订阅

快速排序:

#! /usr/bin/env python
#coding=utf-8

import random,copy

def partition(lst,left,right):
    pivot = lst[left]
    pivot_index = left
    left = left + 1
    while True:
        # when pivot as lst[left] start from left
        while left <= right and lst[left] <= pivot:
            left = left + 1
        while left <= right and lst[right] >= pivot:
            right = right - 1
        if left <= right:
            lst[left],lst[right] = lst[right],lst[left]
        else:
            break
    lst[pivot_index],lst[right] = lst[right],lst[pivot_index]
    return right

def quick_sort_helper(lst,start,end):
    if (start < end):
        split_position = partition(lst,start,end)
        quick_sort_helper(lst,start,split_position-1)
        quick_sort_helper(lst,split_position+1,end)

def quick_sort(lst):
    quick_sort_helper(lst,0,len(lst)-1)
    

def quick_sort_helper2(lst,left,right):
    if left < right:
        pivot_index = right
        pivot = lst[pivot_index]
        i = left
        k = left
        while i < right:
            if lst[i] <= pivot:
                lst[k],lst[i] = lst[i],lst[k]
                k = k + 1
            i = i + 1
        lst[k],lst[pivot_index] = lst[pivot_index],lst[k]
        quick_sort_helper2(lst,left,k-1)
        quick_sort_helper2(lst,k+1,right)
        

def quick_sort2(lst):
    quick_sort_helper2(lst,0,len(lst)-1)
        

if  __name__ == '__main__':
    lst = [random.randint(0,20) for i in range(10)]
    lst2 = copy.deepcopy(lst)
    lst2.sort()
    print(lst)
    print(lst2)
    quick_sort2(lst)
    print(lst)

    

堆排序:

#! /usr/bin/env python
#coding=utf-8

import random,copy

def heap_sort_helper(lst,left,right):
    # max heapify
    current_value = lst[left]
    child = 2 * left + 1
    while (child <= right):
        if (child < right and lst[child] < lst[child+1]):
            child  = child + 1
        if (current_value > lst[child]):
            break
        else:
            lst[(child-1)>>1] = lst[child]
            child = 2 * child + 1
    lst[(child-1)>>1] = current_value
    
def heap_sort(lst):
    # build heap
    for i in range((len(lst)-1)>>1,-1,-1):
        heap_sort_helper(lst,i,len(lst)-1)
    for i in range(len(lst)-1,0,-1):
        lst[i],lst[0] = lst[0],lst[i]
        heap_sort_helper(lst,0,i-1)

if  __name__ == "__main__":
    lst = [random.randint(0,20) for i in range(10)]
    lst2 = copy.deepcopy(lst)
    lst2.sort()
    print(lst)
    print(lst2)
    heap_sort(lst)
    print(lst)
    
    
    

归并排序:

#! /usr/bin/env python
#coding=utf-8
import random

def mergesort(seq):
    mid = len(seq) // 2
    lft,rht = seq[:mid],seq[mid:]
    if len(lft) > 1: 
        lft = mergesort(lft)
    if len(rht) > 1:
        rht = mergesort(rht)
    res = []
    while lft and rht:
        #using pop to reduce data movements
        if lft[-1] >= rht[-1]:
            res.append(lft.pop())
        else:
            res.append(rht.pop())
    res.reverse()
    return (lft or rht) + res

def main():
    seq = [random.randint(0,20) for i in range(10)]
    print(seq)
    seq = mergesort(seq)
    print(seq)
    
if __name__ == "__main__":
    main()
    

    




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值