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

快速排序:

#! /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()
    

    

点赞