《大话数据结构》代码实现——第九章 排序

排序方法总体分为三类:

方法类别 低阶 高阶
插入排序 直接插入排序 希尔排序
选择排序 简单选择排序 堆排序
交换排序 冒泡排序 快速排序
归并排序 归并排序  

在此对每个排序方法做下总结以及实现:


一、插入类排序

1. 直接插入排序:将一个记录插入到已经排好序的有序表中

def zhijiecharu(list):
    if not list:
        return false
    for i in range(1, len(list)):
        tmp = list[i]
        j = i-1
        if tmp < list[j]:
            while(list[j] > tmp and j >= 0):
                list[j+1] = list[j]
                j -= 1
            list[j+1] = tmp
    return list
if __name__ == '__main__':
    array = [49, 38, 65, 97, 76, 13, 27, 49, 55, 4]
    print(zhijiecharu(array))

2. 希尔排序:为了使整个序列变得基本有序,在直接插入基础上,增加了Increment这个变量,先跳着进行一次直接插入排序,这个跳的步幅越来越小,最后一次就是1.

写代码时出错的点:

(1)temp搞不清楚是给新来的数还是下标减increment的数

(2)while里面经常忘了写and j>=0这个条件


def shell_sorted(list):
    if not list:
        print(False)
    incremnt = len(list)
    while incremnt > 1:
        incremnt = incremnt // 3 + 1
        # print(incremnt)
        for i in range(incremnt, len(list)):
            temp = list[i]
            j = i - incremnt
            if list[j] > temp:
                while(list[j] > temp and j >= 0):
                    list[j+incremnt] = list[j]
                    j -= incremnt
                list[j+incremnt] = temp
    return list

if __name__ == '__main__':
    array = [49, 38, 65, 97, 76, 13, 27, 49, 55, 4]
    print(shell_sorted(array))

二、选择排序类

1. 简单选择排序:在当前下标以后,选择最小的数,和当前下标的数作比较并且交换


def easy_select(list):
    if not list:
        return False
    for i in range(len(list)):
        min_ind = i
        min_key = list[i]
        for j in range(i+1,len(list)):
            if list[j] < min_key:
                min_key = list[j]
                min_ind = j
        if i!=min_ind:
            temp = list[i]
            list[i] = min_key
            list[min_ind] = temp
    return list

if __name__ == '__main__':
    array = [49, 38, 65, 97, 76, 13, 27, 49, 55, 4]
    print(easy_select(array))

2. 堆排序:先将整个序列排成大顶堆,将堆顶元素与序列最后一个元素互换,再对前n-1个数字排成大顶堆。

写代码时出错的点:

(1) python中下标不能直接乘以2,应该2*(i+1)-1

(2)while的最后一句有点不太清楚是写外面还是写里面

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

def get_heap(list, start_ind, end_ind):
    j = 2 * (start_ind + 1) - 1
    while(j <= end_ind ):
        temp = list[start_ind]
        if (j+1)<=end_ind and list[j] < list[j+1]:
            j +=1
        if list[j] <= temp:
            break
        else:
            list[start_ind] = list[j]
            list[j] = temp
            start_ind = j
            j = 2*(j+1)-1
    # list[(j+1)//2-1] = temp
    print(list)

def heap_sort(list):
    if not list:
        return False
    for i in range(len(list)//2-1,-1,-1):
        m = len(list)-1
        get_heap(list, i, m)
    for j in range(len(list)-1,-1,-1):
        swap(list, 0, j)
        get_heap(list, 0, j-1)
    return list



if __name__ == '__main__':
    array = [49, 38, 65, 97, 76, 13, 27, 49, 55, 4]
    array2 = [5,1,9,3,7,4,8,6,2]
    # print([i for i in range(3,-1,-1)])
    print(heap_sort(array2))

三、交换类排序

1. 冒泡排序:相邻两个逐个对比

def maopao(list):
    if not list:
        return False
    for i in range(len(list) - 1):
        for j in range(len(list)-2, i-1, -1):
            if list[j] > list[j+1]:
                temp = list[j]
                list[j] = list[j+1]
                list[j+1] = temp
    return list
if __name__ == '__main__':
    array = [9,1,5,8,3,7,4,6,2]
    print(maopao(array))

2. 快速排序:首先选定一个pivotkey并获取其在待排序列中的坐标Pivot,保证pivot左边所有数字均小于pivotkey,右边所有数字均大;根据pivot位置,将待排序列分成左右两个子序列,再对两个子序列进行递归来排序。(排序的交换操作实际上在获取pivot的过程中进行)


def quick_sort(list):
    if not list:
        return False
    qsort(list, 0, len(list)-1)
    return list

def get_pivot_ind(list, low, high):
    # get pivot
    mid = (low + high)//2
    if list[low] > list[high]:
        temp = list[low]
        list[low] = list[high]
        list[high] = temp
    if list[mid] > list[high]:
        temp = list[mid]
        list[mid] = list[high]
        list[high] = temp
    if list[low] < list[mid]:
        list[mid],list[low] = list[low],list[mid]
        # temp = list[mid]
        # list[mid] = list[low]
        # list[low] = temp
    temp = list[low]
    while(low < high):
        while(low < high and list[high] > temp):
            high -= 1
        list[low] = list[high]
        while(low < high and list[low] < temp):
            low += 1
        list[high] = list[low]
    list[low] = temp
    return low

def qsort(list, low, high):

    while (low < high):
        pivotind = get_pivot_ind(list, low, high)
        qsort(list, low, pivotind-1)
        low = pivotind + 1
    return list

if __name__ == '__main__':
    array = [9,1,5,8,3,7,4,6,2]
    print(quick_sort(array))

四、归并类排序

你可能感兴趣的:(数据结构实现)