A stew of sorting algorithms: quick sort, merge sort, bubble sort

1. Quick sort (top-down)

1. Quick sort in-place version

The best-case time complexity : O(nlogn), where logn is the number of levels of recursion, and n is the total time complexity in each level of recursion.
Worst case time complexity : O(n*n)

def quicksort_inplace(left,right): #子数组第一个元素和最后一个元素在原数组中的位置
    if left >= right: #递归终止条件
        return
    l,r,std = left, right, nums[left] #初始化左指针,右指针和基准值
    while r > l:    ##调整元素位置
        while nums[r] >= std and r > l:
            r -= 1
        tmp = nums[l]
        nums[l] = nums[r]
        nums[r] = tmp
        while nums[l] < std and r > l:
            l += 1
        tmp = nums[r]
        nums[r] = nums[l]
        nums[l] = tmp
    quicksort_inplace(left,l)
    quicksort_inplace(l+1,right)
    
nums = [5,3,6,4,1,2,8,7]
quicksort_inplace(0,len(nums)-1)
print(nums)

2. Quick sort space-for-time version

Best case time complexity : less than O(nlogn)

#快速排序
#没有if if else的用法
def quicksort_basic(arr):
    if len(arr) <= 1:
        return arr
    left = []
    right = []
    mid = []
    std = arr[len(arr) // 2]
    for x in arr:
        if x == std:
            mid.append(x)
        if x < std:
            left.append(x)
        #else:
        if x > std:
            right.append(x)
    return quicksort_basic(left) + mid + quicksort_basic(right)

2. Merge sort (bottom-up)

Thought : divide and conquer. Divide and conquer, recursively divide the data into two parts until there is only one element in the array. Merge reorders two sorted arrays. Return a new array. Specifically, the meaning of merging is as follows: Assume there are two already ordered lists , set two pointers to point to the starting elements of the two lists, apply for memory space to create a new empty list, and compare the sizes of the elements pointed to by the two pointers. , adds the smaller element to the new list, then offsets the pointer to the next element of the list, continues comparing the elements pointed to by the two pointers and adds the smaller element to the new list. Until all the data in one list has been added, add the remaining data in the other list to the new list in order. This implements the method of merging two ordered lists into a new ordered list.

Compared with quick sort, the time complexity of merge is more stable : it remains O(nlogn), where logn is the number of recursion levels, and n is the total time complexity in each level of recursion.

#归并排序
nums = [5,3,6,4,1,2,8,7]
def merge_sort(nums):
    if len(nums) <= 1:
        return nums
    left_arr = merge_sort(nums[0:len(nums) // 2])
    right_arr = merge_sort(nums[len(nums) // 2:])
    return merge(left_arr,right_arr)

def merge(left_arr,right_arr):
    rst = []
    pointer1 = 0
    pointer2 = 0
    while pointer1 < len(left_arr) and pointer2 < len(right_arr):
        if left_arr[pointer1] <= right_arr[pointer2]:
            rst.append(left_arr[pointer1])
            pointer1 += 1
        else:
            rst.append(right_arr[pointer2])
            pointer2 += 1
    return rst + right_arr[pointer2:] + left_arr[pointer1:] #技巧:right_arr[pointer2:]中加了冒号就不会报索引超出index的错误
        
rst = merge_sort(nums) 
print(rst)   

3. Bubble sorting

Core principle : Using two layers of for loops, each outer loop can put the smallest element in the unordered element at the beginning of the ordered element. The worst
time complexity : O(n*n)

#冒泡排序
nums = [5,3,6,4,1,2,8,7]
def bubbleSort(nums):
    for i in range(len(nums)):
        for j in range(i+1,len(nums)):
            if nums[j] < nums[i]:
                tmp = nums[j]
                nums[j] = nums[i]
                nums[i] = tmp      
bubbleSort(nums) 
print(nums)   

Refer:
[Python Algorithm Series 2] Quick Sort Algorithm
Quick Sort & Merge Sort - Time Complexity Analysis
Quick Sort and Merge Sort
Python Implements Merge Sort

Guess you like

Origin blog.csdn.net/Rolandxxx/article/details/131404824