基本排序算法(冒泡,选择(希尔),插入,快速,归并,堆,二分查找)(python)

冒泡排序↓

def swap(L,a,b):
    L[a],L[b] = L[b], L[a]


def bubboSort1(array):
    for i in range(0, len(array)):
        for j in range(i+1, len(array)):
            if array[i] > array[j]:
                swap(array,i,j)
    return array


def bubboSort2(array):
    for i in range(0, len(array)):
        for j in range(0,len(array)-1-i):
            if array[j] > array[j+1]:
                swap(array, j, j+1)
    return array

选择排序↓

def selectSort(array):
    for i in range(0, len(array)):
        minValue_index = i
        for j in range(minValue_index+1, len(array)):
            if array[j] < array[minValue_index]:
                minValue_index = j
        if minValue_index != i:
            swap(array,i,minValue_index)
    return array

插入排序↓

def insertSort1(array):
    for i in range(1,len(array)):
        for j in range(0,i)[::-1]:
            if array[j] > array[j+1]:
                swap(array,j,j+1)
    return array


def insertSort2(array):
    for i in range(1,len(array)):
        for j in range(i-1,-1,-1):
            if array[j] > array[j+1]:
                swap(array,j,j+1)
    return array

希尔排序↓

def shellSort(array):
    gap = len(array)

    while gap > 0:
        for i in range(1, len(array)):
            for j in range(i - 1, -1, -1):
                if array[j] > array[j + 1]:
                    swap(array, j, j + 1)
        gap = gap//2

    return array

快速排序↓


def quickSort(array):
    if len(array) < 2:
        return array
    else:
        pivot_index = 0
        pivot = array[pivot_index]
        left = [element for element in array[pivot_index+1:] if element<=pivot]
        right = [element for element in array[pivot_index+1:] if element>pivot]
        return quickSort(left)+[pivot]+quickSort(right)

归并排序↓

# 分治法
def _mergeSort(a_list,b_list):
    a,b=0,0
    res_list = []
    while a<len(a_list) and b<len(b_list):
        if a_list[a] > b_list[b]:
            res_list.append(b_list[b])
            b+=1
        else:
            res_list.append(a_list[a])
            a+=1
    if a<len(a_list):
        res_list.extend(a_list[a:])
    else:
        res_list.extend(b_list[b:])
    return res_list


def mergeSort(array):
    if len(array) < 2:
        return array
    else:
        mid = int(len(array)/2)
        left = mergeSort(array[mid:])
        right = mergeSort(array[:mid])
        return _mergeSort(left,right)

堆排序↓

def heapSort(array):
    from heapq import heappop,heappush
    heap = []
    for i in array:
        heappush(heap, i)
    return [heappop(heap) for _ in range(0,len(array))]

################################################

def Max_Heapify(arr, arr_length, root):
    left = root * 2 + 1
    right = root * 2 + 2
    larger = root
    if left < arr_length and arr[larger] < arr[left]:
        larger = left
    if right < arr_length and arr[larger] < arr[right]:
        larger = right
    if larger != root:
        arr[larger],arr[root] = arr[root], arr[larger]
        Max_Heapify(arr,arr_length,larger)


def Build_Heap(array):
    arr_length = len(array)
    for i in range(arr_length//2, -1, -1):
        Max_Heapify(array,arr_length,i)


def HeapSort2(array):
    Build_Heap(array)
    for i in range(len(array)-1, -1, -1):
        array[0],array[i] = array[i],array[0]
        Max_Heapify(array,i,0)
    return array

二分查找↓

def two_search(sort_array, v):
    if len(sort_array) < 1:
        return -1
    start = 0
    end = len(sort_array) - 1
    while start <= end:
        mid = int((start+end)/2)
        if sort_array[mid] == v:
            return mid
        elif sort_array[mid] > v: #值在中值左侧
            end = mid - 1
        else:                       #值在中值左侧
            start = mid + 1
    return -1
posted @ 2019-06-30 20:27  lilied  阅读(213)  评论(0编辑  收藏  举报