Randomized quickSort

本文介绍了一种使用C++实现的快速排序算法,并详细展示了如何通过随机化选取枢轴来优化排序过程,提高算法效率。代码示例清晰地说明了随机化快速排序的具体实现方法。

C/C++中产生随机数(rand,srand用法)
http://blog.youkuaiyun.com/PYPARA/article/details/54773968
Quick sort
http://blog.youkuaiyun.com/pypara/article/details/54773861

C++:

int partition(int *a, int lhs, int rhs);
int RandomizedPartition(int *a, int lhs, int rhs) {
    int rad = (rand() % (rhs - lhs + 1)) + lhs;
    int pivot = a[rad];
    int temp = a[lhs];
    a[lhs] = pivot;
    a[rad] = temp;
    return partition(a, lhs, rhs);

}

void RandomizedQuickSort(int *a, int lhs, int rhs) {
    if (lhs >= rhs) {
        return;
    }
    int q = RandomizedPartition(a, lhs, rhs);
    RandomizedQuickSort(a, lhs, q);
    RandomizedQuickSort(a, q + 1, rhs);
}


int partition(int *a, int lhs, int rhs) {
    int i = lhs;
    int j = lhs + 1;
    int pivot = a[lhs];
    while (j <= rhs) {
        if (a[j] >= pivot) {
            ++j;
        }
        else {
            ++i;
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
            ++j;
        }
    }
    int temp2 = a[i];
    a[i] = pivot;
    a[lhs] = temp2;
    return i;
}



void QuickSort(int *a, int lhs, int rhs) {
    if (lhs >= rhs) {
        return;
    }
    int q = partition(a, lhs, rhs);
    QuickSort(a, lhs, q);
    QuickSort(a, q + 1, rhs);
}

Text:

#include<iostream>
int partition(int *a, int lhs, int rhs);
int RandomizedPartition(int *a, int lhs, int rhs) {
    int rad = (rand() % (rhs - lhs + 1)) + lhs;
    int pivot = a[rad];
    int temp = a[lhs];
    a[lhs] = pivot;
    a[rad] = temp;
    return partition(a, lhs, rhs);

}

void RandomizedQuickSort(int *a, int lhs, int rhs) {
    if (lhs >= rhs) {
        return;
    }
    int q = RandomizedPartition(a, lhs, rhs);
    RandomizedQuickSort(a, lhs, q);
    RandomizedQuickSort(a, q + 1, rhs);
}


int partition(int *a, int lhs, int rhs) {
    int i = lhs;
    int j = lhs + 1;
    int pivot = a[lhs];
    while (j <= rhs) {
        if (a[j] >= pivot) {
            ++j;
        }
        else {
            ++i;
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
            ++j;
        }
    }
    int temp2 = a[i];
    a[i] = pivot;
    a[lhs] = temp2;
    return i;
}



void QuickSort(int *a, int lhs, int rhs) {
    if (lhs >= rhs) {
        return;
    }
    int q = partition(a, lhs, rhs);
    QuickSort(a, lhs, q);
    QuickSort(a, q + 1, rhs);
}

int main() {

    int a[22] = { 9,8,7,6,5,4,3,2,1,0,5,65,435,224,234,84,642,32,13,87,215,21 };
    RandomizedQuickSort(a, 0, 21);
    for (auto c : a) {
        std::cout << c << " ";
    }
    std::cout << std::endl;
    return 0;
}

快速排序(Quicksort)是一种广泛使用的排序算法,其平均时间复杂度为 O(n log n),但在最坏情况下可能退化为 O(n²)。为了在 Python 中优化快速排序,可以采用多种技术来提高其性能并减少最坏情况的发生概率。 ### 三重切分快速排序(3-Way Partitioning) 在传统快速排序中,如果输入数据中存在大量重复元素,性能可能会下降。三重切分快速排序将数组划分为三部分:小于、等于和大于基准值的元素。这种方法可以显著减少递归深度,并提高包含重复元素的数据集的排序效率。 ```python def quick_sort_3way(arr): if len(arr) <= 1: return arr elif len(arr) == 2: return [min(arr), max(arr)] else: left, mid, right = [], [], [] pivot = arr[len(arr) // 3] # 选取中间位置的元素作为基准 for x in arr: if x < pivot: left.append(x) elif x == pivot: mid.append(x) else: right.append(x) return quick_sort_3way(left) + mid + quick_sort_3way(right) ``` ### 随机化基准选择(Randomized Pivot) 为了减少最坏情况的发生概率,可以在选取基准元素时使用随机化策略。这样可以避免针对特定输入(如已排序数组)导致的性能下降问题。 ```python import random def quick_sort_randomized(arr): if len(arr) <= 1: return arr else: pivot = random.choice(arr) # 随机选择基准元素 left = [x for x in arr if x < pivot] mid = [x for x in arr if x == pivot] right = [x for x in arr if x > pivot] return quick_sort_randomized(left) + mid + quick_sort_randomized(right) ``` ### 小数组切换为插入排序(Hybrid with Insertion Sort) 对于小规模数据集,插入排序通常比快速排序更快。因此,在递归过程中,当子数组的大小小于某个阈值(例如 10 个元素),可以切换为插入排序以减少递归开销。 ```python def insertion_sort(arr): for i in range(1, len(arr)): key = arr[i] j = i - 1 while j >= 0 and arr[j] > key: arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key return arr def quick_sort_hybrid(arr, threshold=10): if len(arr) <= threshold: return insertion_sort(arr) else: pivot = arr[len(arr) // 2] left = [x for x in arr if x < pivot] mid = [x for x in arr if x == pivot] right = [x for x in arr if x > pivot] return quick_sort_hybrid(left, threshold) + mid + quick_sort_hybrid(right, threshold) ``` ### 尾递归优化(Tail Recursion Elimination) Python 对递归深度有限制,默认递归深度限制为 1000。为了避免栈溢出问题,可以对快速排序进行尾递归优化,将递归调用转换为迭代方式,从而减少栈帧数量。 ```python def quick_sort_tail_recursive(arr): stack = [arr] result = [] while stack: current = stack.pop() if len(current) <= 1: result.extend(current) continue pivot = current[len(current) // 2] left = [x for x in current if x < pivot] mid = [x for x in current if x == pivot] right = [x for x in current if x > pivot] stack.append(right) stack.append(mid) stack.append(left) return result ``` ### 利用 NumPy 进行向量化操作(Vectorization with NumPy) 如果排序的数据集非常大且需要高性能,可以利用 NumPy 的向量化操作来替代 Python 原生的列表操作。NumPy 内部实现为 C 语言级别的操作,因此执行速度更快。 ```python import numpy as np def numpy_quicksort(arr): return np.sort(arr, kind='quicksort') # 使用 NumPy 的快速排序实现 ``` ### 总结 通过三重切分、随机化基准、混合插入排序、尾递归优化以及使用 NumPy 等技术,可以在 Python 中显著提升快速排序的性能。这些优化策略不仅提高了算法的效率,还增强了对不同输入模式的适应能力。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值