python基础查漏补缺6--排序算法

#! /usr/bin/env python3
# -*- coding=utf-8 -*-

'''
@Date: 2017-10-10
@Author: Henry
Python各种排序算法
'''

def bubble_sort(ls):
    '''
    冒泡排序
    '''
    if ls == None:
        return []
    else:
        for j in range(len(ls)-1):
            for k in range(len(ls)-j-1):
                if ls[k] > ls[k+1]:
                    ls[k], ls[k+1] = ls[k+1], ls[k]
        return ls

def fast_sort(ls):
    '''
    使用python的列表生成式的快速排序
    '''
    if len(ls) < 2:
        return ls
    else:
        return fast_sort([x for x in ls[1:] if x <= ls[0]]) \
    + [ls[0]] + fast_sort([y for y in ls[1:] if y > ls[0]])


'''
标准快速排序
'''
def partition(L, first, last):
    pivot = L[first]

    leftmark = first + 1
    rightmark = last

    while True:
        while L[leftmark] <= pivot:
            if leftmark == rightmark:
                break
            leftmark += 1

        while L[rightmark] > pivot:
            rightmark -= 1

        if leftmark < rightmark:
            L[leftmark], L[rightmark] = L[rightmark], L[leftmark]
        else:
            break

    L[first], L[rightmark] = L[rightmark], L[first]
    return rightmark

def rand_partition(L, first, last):
    '''
    随机取得比较用的随机值
    '''
    import random
    rand_num = random.randint(first, last)
    L[first], L[rand_num] = L[rand_num], L[first]
    pivot = L[first]

    leftmark = first + 1
    rightmark = last

    while True:
        while L[leftmark] <= pivot:
            if leftmark == rightmark:
                break
            leftmark += 1

        while L[rightmark] > pivot:
            rightmark -= 1

        if leftmark < rightmark:
            L[leftmark], L[rightmark] = L[rightmark], L[leftmark]
        else:
            break

    L[first], L[rightmark] = L[rightmark], L[first]
    return rightmark

def std_quick_sort(L, first, last):
    if first < last:
        split = partition(L, first, last)
        # split = rand_partition(L, first, last)
        std_quick_sort(L, first, split-1)
        std_quick_sort(L, split+1, last)

def stdQuickSort(L):
    std_quick_sort(L, 0, len(L)-1)

# 标准快速排序结束

'''
选择排序,每次从剩余列表中选择最大值放在最后面,冒泡排序的升级,减少了swap操作
'''
def selection_sort(L):
    for pos in range(len(L)-1, 0, -1):
        positionMax = 0
        for location in range(1, pos+1):
            if L[location] > L[positionMax]:
                positionMax = location
        L[pos], L[positionMax] = L[positionMax], L[pos]


'''
插入排序
'''
def insertion_sort(alist):
    for index in range(1, len(alist)):
        currentvalue = alist[index]
        position = index

        while position > 0 and alist[position-1] > currentvalue:
            alist[position] = alist[position-1]
            position = position - 1

        alist[position] = currentvalue

'''
shell 排序
'''

def shell_sort(alist):
    sublist_count = len(alist) // 2
    while sublist_count > 0:
        for startposition in range(sublist_count):
            gap_insertion_sort(alist, startposition, sublist_count)
        sublist_count = sublist_count // 2

def gap_insertion_sort(alist, start, gap):
    for index in range(start, len(alist), gap):
        currentvalue = alist[index]
        position = index

        while position > start and alist[position-gap] > currentvalue:
            alist[position] = alist[position-gap]
            position = position - gap

        alist[position] = currentvalue

'''
归并排序
'''
def merge_sort(alist):
    if len(alist) < 2:
        return alist
    middle = len(alist) // 2
    left = merge_sort(alist[:middle])
    right = merge_sort(alist[middle:])
    return merge(left, right)

def merge(left, right):
    i = 0
    j = 0
    k = 0

    result = []
    while i < len(left) and j < len(right):
        if left[i] < right[j]:
            result.append(left[i])
            i += 1
        else:
            result.append(right[j])
            j += 1
        k += 1
    result += right[j:]
    result += left[i:]
    return result


if __name__ == '__main__':
    # print(bubble_sort([]))
    # print(bubble_sort([2, 1, 3, 45, 5, 5]))
    # print(fast_sort([2, 1, 3, 45, 5, 5]))
    # print(fast_sort([2, 2, 2, 2]))
    import random
    ls = [random.randint(1, 100) for x in range(0, 100)]
    # import time
    # t1 = time.time()
    # stdQuickSort(ls)
    # # print(ls)
    # t2 = time.time()    

    # print(t2 - t1)
    # selection_sort(ls)
    # insertion_sort(ls)
    # shell_sort(ls)
    print(merge_sort(ls))

  

posted @ 2017-10-13 10:27  燃灯胡同  阅读(272)  评论(0编辑  收藏  举报