嘿,我是Matlab的新手,我写了一个简单的快速排序代码,但是对于一些数组,大多数是较长的数组,我的递归失败,并给出以下错误:“达到500的最大递归限制。使用set(0,'RecursionLimit',N)更改限制。请注意,超过可用堆栈空间可能会导致MATLAB和/或计算机崩溃。
查询中出错“
我认为我的基本情况可能是错误的,但我不知道我做错了什么。如果能得到一些帮助,我们将不胜感激。
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;发布于 2014-09-28 20:19:18
我注意到的第一件事是,您没有包括停止条件,这意味着算法永远不会停止,甚至超过任何有限限制。第二,返回一个透视表值,同时将该值用作下一次迭代的透视索引。
包含一个停止条件并返回索引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顺便说一下,你可以通过使用更少的变量来大大缩短你的代码。如果包含对参数数量的检查,则只需将数组本身作为参数即可调用快速排序。
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]);
endhttps://stackoverflow.com/questions/26069963
复制相似问题