Matlab最大递归极限达到500误差

Matlab最大递归极限达到500误差,matlab,recursion,quicksort,Matlab,Recursion,Quicksort,嘿,我是Matlab新手,我为快速排序编写了一个简单的代码,但对于一些数组(主要是较长的数组),我的递归失败,并给出以下错误: 已达到最大递归限制500。请使用set(0,'RecursionLimit',N)更改 限制。请注意,超过可用堆栈空间可能会使MATLAB和/或系统崩溃 你的电脑 quic中的错误“ 我认为我的基本情况可能是错误的,但我不知道我做错了什么。我们将非常感谢您的帮助 function A = quicksort(A,left,right) [A, pivot] = Pa

嘿,我是Matlab新手,我为快速排序编写了一个简单的代码,但对于一些数组(主要是较长的数组),我的递归失败,并给出以下错误: 已达到最大递归限制500。请使用set(0,'RecursionLimit',N)更改 限制。请注意,超过可用堆栈空间可能会使MATLAB和/或系统崩溃 你的电脑

quic中的错误“

我认为我的基本情况可能是错误的,但我不知道我做错了什么。我们将非常感谢您的帮助

function A = quicksort(A,left,right)


[A, pivot] = PartitionPivot(A, left, right); %chosen pivot
A = quicksort(A, left, pivot-1); %scan from the left of the array until an element greater than pivot is found and swap it with the element less than the pivot on the right of the pivot
A = quicksort(A, pivot+1, right); %scan from the right of the array until an element less than   pivot is found and swap it with the element greater than the pivot on the left of the pivot
end

function [sortedSub_array,Pivot] = PartitionPivot(Sub_array,left_index,right_index)

% Initialization
S = Sub_array;
left = left_index;
right = right_index;

P = S(left); %pivot
i = left+1;

% Partition
for j = i:right 
    if S(j) < P 
        temp1 = S(j);
        S(j) = S(i);
        S(i) = temp1;
        i = i+1; %increment i only when swap occurs
    end
end
swap1 = S(left);
S(left) = S(i-1);
S(i-1) = swap1;

sortedSub_array = S;
Pivot = P;
函数A=快速排序(A,左,右)
[A,pivot]=分区pivot(A,左,右);%选定轴心
A=快速排序(A,左,轴-1);%从数组的左侧扫描,直到找到大于枢轴的元素,并将其与枢轴右侧小于枢轴的元素交换
A=快速排序(A,轴+1,右);%从数组右侧扫描,直到找到小于枢轴的元素,并将其与枢轴左侧大于枢轴的元素交换
结束
函数[sortedSub_数组,Pivot]=分区Pivot(Sub_数组,左索引,右索引)
%初始化
S=子_数组;
左=左指数;
右=右指数;
P=S(左);%支点
i=左+1;
%分割
对于j=i:对
如果S(j)

我注意到的第一件事是,您没有包含停止标准,这意味着算法永远不会停止,并且确实会超过任何有限限制。第二,返回一个pivot值,同时将该值用作下一次迭代的pivot索引

包括停止条件并返回索引i而不是值p可以解决您的问题:

function A = quicksort(A,left,right)


if left < right
    [A, pivot] = PartitionPivot(A, left, right); %chosen pivot
    A = quicksort(A, left, pivot-1); %scan from the left of the array until an element greater than pivot is found and swap it with the element less than the pivot on the right of the pivot
    A = quicksort(A, pivot+1, right); %scan from the right of the array until an element less than   pivot is found and swap it with the element greater than the pivot on the left of the pivot
end

end

function [sortedSub_array,PivotIndex] = PartitionPivot(Sub_array,left_index,right_index)

% Initialization
S = Sub_array;
left = left_index;
right = right_index;

P = S(left); %pivot
i = left+1;

% Partition
for j = i:right
    if S(j) < P 
        temp1 = S(j);
        S(j) = S(i);
        S(i) = temp1;
        i = i+1; %increment i only when swap occurs
    end
end
swap1 = S(left);
S(left) = S(i-1);
S(i-1) = swap1;

sortedSub_array = S;
PivotIndex = i-1;

end
函数A=快速排序(A,左,右)
如果左<右
[A,pivot]=分区pivot(A,左,右);%选定轴心
A=快速排序(A,左,轴-1);%从数组的左侧扫描,直到找到大于枢轴的元素,并将其与枢轴右侧小于枢轴的元素交换
A=快速排序(A,轴+1,右);%从数组右侧扫描,直到找到小于枢轴的元素,并将其与枢轴左侧大于枢轴的元素交换
结束
结束
函数[sortedSub_数组,PivotIndex]=分区Pivot(Sub_数组,左_索引,右_索引)
%初始化
S=子_数组;
左=左指数;
右=右指数;
P=S(左);%支点
i=左+1;
%分割
对于j=i:对
如果S(j)

顺便说一下,通过使用较少的变量,您可以显著缩短代码。通过检查参数的数量,可以仅使用数组本身作为参数调用快速排序

function A = quicksort(A,left,right)

if nargin == 1
    left = 1;
    right = numel(A);
end

if left < right
    [A, pivot] = PartitionPivot(A, left, right); %chosen pivot
    A = quicksort(A, left, pivot-1); %scan from the left of the array until an element greater than pivot is found and swap it with the element less than the pivot on the right of the pivot
    A = quicksort(A, pivot+1, right); %scan from the right of the array until an element less than   pivot is found and swap it with the element greater than the pivot on the left of the pivot
end

end

function [A, i] = PartitionPivot(A,left,right)

P = A(left); % pivot

A([right left]) = A([left right]);
i = left;

% Partition
for j = left:right-1
    if A(j) < P 
        A([j i]) = A([i j]);
        i = i+1; % increment i only when swap occurs
    end
end
A([right i]) = A([i right]);

end
函数A=快速排序(A,左,右)
如果nargin==1
左=1;
右=努梅尔(A);
结束
如果左<右
[A,pivot]=分区pivot(A,左,右);%选定轴心
A=快速排序(A,左,轴-1);%从数组的左侧扫描,直到找到大于枢轴的元素,并将其与枢轴右侧小于枢轴的元素交换
A=快速排序(A,轴+1,右);%从数组右侧扫描,直到找到小于枢轴的元素,并将其与枢轴左侧大于枢轴的元素交换
结束
结束
函数[A,i]=分区轴(A,左,右)
P=A(左);%支点
A([右-左])=A([左-右]);
i=左;
%分割
对于j=左:右-1
如果A(j)