快速排序算法的MATLAB实现如下:

function sortedArray = quicksort(array)
    if length(array) <= 1
        sortedArray = array;
        return;
    end
    
    pivot = array(1);
    lower = array(array < pivot);
    upper = array(array > pivot);
    
    sortedArray = [quicksort(lower), pivot, quicksort(upper)];
end

使用示例:

array = [5, 9, 1, 3, 2, 8, 4, 7, 6];
sortedArray = quicksort(array);
disp(sortedArray);

输出结果:

1     2     3     4     5     6     7     8     9
使用MATLAB实现快速排序算法

原文地址: https://www.cveoy.top/t/topic/hOvY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录