繁体   English   中英

结合了插入排序和快速排序的混合算法

[英]Hybrid algorithm that combines Insertion Sort and Quick Sort

我正在尝试实现一种结合了插入排序和快速排序的混合算法,以这种方式获得两者所需的功能,以便当元素在快速排序步骤中少于 50 时,使用插入排序,大于 50使用快速排序算法合并元素。 如果元素小于 50,我尝试使用插入排序对 pivot_index 左侧的元素和 pivot_index 右侧的元素进行排序,方法是将原始列表的切片实例作为参数传递到我的 insertionSort function 中,但我的列表仍然是相同,分区但未排序。 任何解决此问题的指标将不胜感激。

from random import randint

def insertionSort(arr):
    
    for i in range(1, len(arr)):
        key = arr[i]
        j = i - 1
        
        while j >= 0 and key < arr[j]:
            arr[j+1] = arr[j]
            j = j - 1
            
        arr[j+1] = key
        
def partition(a, low, high): #lomuto partition scheme
    i = low - 1 #directly to the left of our specified low-high range
    pivot = a[high] #pivot is the last value in our range, the index equal to high parameter
    for j in range(low, high):
        if a[j] <= pivot:
            i += 1
            a[i], a[j] = a[j], a[i]
    a[i+1], a[high] = a[high], a[i+1]
    return i+1

def quicksort_inplace(a, low=0, high=None):
    if high == None:
        high = len(a) - 1
        
    if low < high:
        pivot_index = partition(a, low, high) #partition around pivot
        print("The pivot_index is:", pivot_index)
        print("Low is:", low)
        print("High is:", high)
        
        if pivot_index - low <= 50:
            insertionSort(a[low:pivot_index])
            
        elif high - pivot_index <= 50:
            insertionSort(a[pivot_index+1:high])
            
        else:
            quicksort_inplace(a, low, pivot_index - 1) #sort lower half
            quicksort_inplace(a, pivot_index + 1, high) #sort upper half
        
def create_arr(size=10):
    return [randint(0,50) for _ in range(size)]

arr = create_arr()
print(arr)
quicksort_inplace(arr)
print()
print("The sorted array is:", arr)

    

对列表进行切片会产生原始子列表的浅表副本,因此不会通过将切片实例传递给insertionSort来修改原始列表。 这就是为什么在您的代码中分区后列表没有排序的原因。

要就地应用插入排序,您可以将lowhigh传递为 arguments:

def insertion_sort(arr, low, high):
    for i in range(low + 1, high + 1):
        j = i
        while j > low and arr[j] < arr[j - 1]:
            arr[j], arr[j - 1] = arr[j - 1], arr[j]
            j -= 1

在分区之前检查大小是否低于阈值也会更有效。 quicksort_inplace function 可以重写如下(本例中THRESHOLD = 50 ):

def quicksort_inplace(a, low=0, high=None):
    if high is None:
        high = len(a) - 1

    if low < high:
        if high - low + 1 < THRESHOLD:
            # Size of the subarray is less than the threshold, insertion sort
            insertion_sort(a, low, high)
            return
        # Size of the subarray is greater than the threshold, quicksort
        pivot_index = partition(a, low, high)
        print("The pivot_index is:", pivot_index)
        print("Low is:", low)
        print("High is:", high)
        quicksort_inplace(a, low, pivot_index - 1)
        quicksort_inplace(a, pivot_index + 1, high)

如果阈值是 50,测试数组显然应该比 10 大得多才能正确测试 function,否则你只是在测试插入排序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM