Python简单排序算法的实现

一些简单Python算法实现,留待复习使用

https://python-data-structures-and-algorithms.readthedocs.io/zh/latest/

 

# -*- coding : utf-8 -*-
# Author : Vick.Pan Create at 2019/7/25 11:16
# File   : sortmethod.py
import random

#冒泡排序
def bubble_sort(seq):  # O(n^2), n(n-1)/2 = 1/2(n^2 + n)
    n = len(seq)
    for i in range(n-1):
        print(seq)    # 我打印出来让你
        for j in range(n-1-i):  # 这里之所以 n-1 还需要 减去 i 是因为每一轮冒泡最大的元素都会冒泡到最后,无需再比较
            if seq[j] > seq[j+1]:
                seq[j], seq[j+1] = seq[j+1], seq[j]
    print(seq)

#选择排序
def select_sort(seq):
    n = len(seq)
    for i in range(n):
        print(seq)
        min_idx = i
        for j in range(i+1, n):
            if seq[j] < seq[min_idx]:
                min_idx = j
        if min_idx != i:
            seq[i], seq[min_idx] = seq[min_idx],seq[i]

#插入排序
def insert_sort(seq):
    n = len(seq)
    print(seq)
    for i in range(1, n):
        value = seq[i] #从1开始,每次保存seq【i】
        pos = i
        while pos >0 and value < seq[pos - 1]:  #若位置大于零,值小于后面元素的值,往前挪一位
            seq[pos] = seq[pos - 1 ]
            pos -= 1
        seq[pos] = value
        print(seq)

#归并排序
def merge_sort(seq):
    if len(seq) <= 1:   # 只有一个元素是递归出口
        return seq
    else:
        mid = int(len(seq)/2)
        left_half = merge_sort(seq[:mid])
        right_half = merge_sort(seq[mid:])
       # 合并两个有序的数组
        new_seq = merge_sorted_list(left_half, right_half)
        return new_seq

def merge_sorted_list(sorted_a, sorted_b):
    """ 合并两个有序序列,返回一个新的有序序列

    :param sorted_a:
    :param sorted_b:
    """
    length_a, length_b = len(sorted_a), len(sorted_b)
    a = b = 0
    new_sorted_seq = list()

    while a < length_a and b < length_b:
        if sorted_a[a] < sorted_b[b]:
            new_sorted_seq.append(sorted_a[a])
            a += 1
        else:
            new_sorted_seq.append(sorted_b[b])
            b += 1

    # 最后别忘记把多余的都放到有序数组里
    if a < length_a:
        new_sorted_seq.extend(sorted_a[a:])
    else:
        new_sorted_seq.extend(sorted_b[b:])

    return new_sorted_seq

#快速排序
def quicksort_inplace(array, beg, end):    # 注意这里我们都用左闭右开区间,end 传入 len(array)
    if beg < end:    # beg == end 的时候递归出口
        pivot = partition(array, beg, end)
        quicksort_inplace(array, beg, pivot)
        quicksort_inplace(array, pivo


def partition(array, beg, end):
    pivot_index = beg
    pivot = array[pivot_index]
    left = pivot_index + 1
    right = end - 1    # 开区间,最后一个元素位置是 end-1     [0, end-1] or [0: end),括号表示开区间


   while True:
        # 从左边找到比 pivot 大的
        while left <= right and array[left] < pivot:
            left += 1

        while right >= left and array[right] >= pivot:
            right -= 1
        if left > right:
            break
        else:
            array[left], array[right] = array[right], array[left]

    array[pivot_index], array[right] = array[right], array[pivot_index]
    return right   # 新的 pivot 位置


def test_select():
    seq = list(range(10))
    random.shuffle(seq)
    insert_sort(seq)
    assert seq == sorted(seq)

test_select()

 

你可能感兴趣的:(python,Python)