我如何在 MATLAB 中优化这个冒泡排序算法,或者用其他更有效的方法替换它?

How can I optimise this Bubble Sort algorithm in MATLAB, or replace it by some other more efficient method?

我想知道如何优化我在 MATLAB 代码的一部分中制作的冒泡排序算法,或者用其他更有效的方法(如 QuickSort、TimSort 或 Merge Sort)替换它 在这种情况下,使用了冒泡排序,因为当要排序的数组中的元素被交换时,另一个称为 categoria 的向量中的元素的位置也必须被交换 谢谢

% Boolean variable (flag) to enter and exit the while loop
sigueBucle= true;
% The categories and the distance between each point are sorted
[~, N]= size(dataBase);
while (sigueBucle == true)
    sigueBucle= false;
    % Bubble sort algorithm (sinking sort)
    for i = 2 : N
        if (diferencias(i-1) > diferencias(i))
            % Swap the place of the elements of the array categoria
            auxiliar_etiqueta= categoria(i);
            categoria(i)= categoria(i-1);
            categoria(i-1)= auxiliar_etiqueta;
            % Swap the place of the elements of the distance array
            auxiliar_distanc= diferencias(i);
            diferencias(i)= diferencias(i-1);
            diferencias(i-1)= auxiliar_distanc;
            % Change sigueBucle to T (true) to re-enter the loop
            sigueBucle= true;
        end
    end
end

使用 standard sort function,并捕获其第二个输出“ix”以获得索引向量,该向量将允许您相应地重新排序其他数组。

x = % ... some data ...
y = % ... another vector that needs to be kept ordered like x
[x,ix] = sort(x);  % Sort x
y = y(ix); % Apply the same re-ordering to y