Basic sorting algorithms (bubble, select (Hill), insert, fast, merge, heap, binary search)

Bubble sort ↓

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

Select sort ↓

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

Insertion sort ↓

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

Shell sort ↓

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

Quick Sort ↓


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)

Merge sort ↓

# 分治法
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)

Heap sort ↓

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))]

Binary search ↓

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

Guess you like

Origin www.cnblogs.com/lilied/p/11111070.html