排序算法

1)冒泡排序:逐个比较相邻的两个元素并排序(稳定)
2)选择排序:对于未排序数据,寻找最小元素放到已排序序列末尾(不稳定)
3)插入排序:对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入(稳定)
4)归并排序:使用分治法递归地将两个已排序序列合并成一个序列(稳定)
5)快速排序:使用分治法递归地把小于基准值元素的子数列和大于基准值元素的子数列排序(原地分区版本不稳定)
6)希尔排序:递减步长插入排序(将数组列在一个表中并对列进行插入排序,然后逐步减少步长到1)(不稳定)
7)堆排序: 先创建最大堆,然后递归地移除根结点后做最大堆调整(不稳定)

快速排序优化:
1)使用原地分区减少额外存储空间
2)使用尾递归技术减少递归的深度
3)选择基准优化已有序数列的情况
4)三路基数优化数列重复率高的情况

#!/usr/bin/env python3.3
# -*- coding: utf-8 -*-
# Copyright 2013

def bubble_sort(data):
    for j in range(len(data)-1, 0, -1):
        for i in range(0, j):
            if(data[i] > data[i+1]):
                data[i], data[i+1] = data[i+1], data[i]
    return data


def select_sort(data):
    for j in range(len(data) - 1):
        min = j
        for i in range(j + 1, len(data)):
            if(data[i] < data[min]):
                min = i
        if(j != min):
            data[j], data[min] = data[min], data[j]
    return data


def insert_sort(data):
    for j in range(1, len(data)):
        for i in range(j-1, -1, -1):
            if(data[i] <= data[j]):
                data[i+1:j+1] = data[j:j+1] + data[i+1:j]
                break
            if(i == 0):
                data[:j+1] = data[j:j+1] + data[:j]
    return data


def merge(l1, l2):
    final = []
    while l1 and l2:
        if l1[0] <= l2[0]:
            final.append(l1.pop(0))
        else:
            final.append(l2.pop(0))
    return final + l1 + l2


def merge_sort(data):
    if len(data) <= 1:
        return data
    mid = len(data) // 2
    return merge(merge_sort(data[:mid]), merge_sort(data[mid:]))


def partition(data, left, right):
    pivot_index = (left + right) // 2
    pivot_value = data[pivot_index]
    data[pivot_index], data[right] = data[right], data[pivot_index]
    store_index = left
    for i in range(left, right):
        if data[i] < pivot_value:
            data[i], data[store_index] = data[store_index], data[i]
            store_index += 1
    data[right], data[store_index] = data[store_index], data[right]
    return store_index


def quick_sort(data):
    if(data):
        return quick_sort([x for x in data[1:] if x < data[0]])\
                + data[0:1]\
                + quick_sort([x for x in data[1:] if x >= data[0]])
    return []


def quick_sort(data, left, right):
    if left < right:
        pivot_index = partition(data, left, right)
        quick_sort(data, left, pivot_index-1)
        quick_sort(data, pivot_index+1, right)
    return data


def quick_sort(data, left, right):
    while left < right:
        pivot_index = partition(data, left, right)
        quick_sort(data, left, pivot_index-1)
        left = pivot_index + 1
    return data


def heapify(data, i, size):
    left = i * 2 + 1
    right = i * 2 + 2
    largest = i
    if(left < size and data[left] > data[i]):
        largest = left
    if(right < size and data[right] > data[largest]):
        largest = right
    if(largest != i):
        data[i], data[largest] = data[largest], data[i]
        heapify(data, largest, size)


def heap_sort(data):
    for i in range(len(data)//2, -1, -1):
        heapify(data, i, len(data))
    for i in range(len(data)-1, 0, -1):
        data[0], data[i] = data[i], data[0]
        heapify(data, 0, i)
    return data


def shell_sort(data):
    gap = len(data) // 3 + 1
    while gap > 0:
        for j in range(gap, len(data)):
            pos = j
            key = data[j]
            for i in range(j-gap, -1, -gap):
                if data[i] > key:
                    pos = i
                    data[i+gap] = data[i]
                else:
                    break
            data[pos] = key
        gap //= 2
    return data

8)计数排序:用额外的数组记录每个值的个数,再通过该数组的值将待排序元素放到正确的位置
9)桶排序: 将待排序元素分到有限数量的桶子里,每个桶子再分别排序,再按顺序从桶子取回数据

#!/usr/bin/env python3.3
# -*- coding:utf-8 -*-
# Copyright 2013

def counting_sort(data, min_value, max_value):
    final = [0 for x in range(len(data))]
    counter = [0 for x in range(max_value - min_value + 1)]
    for x in data:
        counter[x-min_value] += 1
    for i in range(1, len(counter)):
        counter[i] += counter[i-1]
    '''
    for x in data:
        final[counter[x-min_value]-1] = x
        counter[x-min_value] -= 1
    '''
    for i in range(len(data)-1, -1, -1):
        final[counter[data[i]-min_value]-1] = data[i]
        counter[data[i]-min_value] -= 1
    return final


def bucket_sort(data, bucket_count, bucket_size):
    final = []
    buckets = []
    for i in range(bucket_count):
        buckets.append([])
    for value in data:
        pos = value // bucket_size
        if pos < bucket_count - 1:
            buckets[pos].append(value)
        else:
            buckets[bucket_count-1].append(value)
    for i in range(bucket_count):
        buckets[i].sort()
        final += buckets[i]
    return final

 

 

 

posted @ 2013-03-02 22:24  Leung文  阅读(194)  评论(0编辑  收藏  举报