-1
我需要編寫一個嵌入了quicksort算法的遞歸函數。 更新新邊界時遇到問題。 y是矩陣,m是需要排序的行的數量。請幫助...quicksort matlab,遞歸函數
function [y]=quicksort(y,left,right,m)
i=left;
j=right;
num=randi(length(y)); % pick a random element in the array as pivot
pivot=y(m,num);
if i <= j %find the element fits criteria below before i overlaps j.
while y(m,i) < pivot
i = i + 1;
end
while y(m,j) > pivot
j = j - 1;
end
ytmp=y(:,j);
y(:,j)=y(:,i);
y(:,i)=ytmp;
i = i + 1;
j = j - 1;
%swap the positions of the two elements when y(m,j) < pivot < y(m,i)
else
return
end
return
[y]=quicksort(y,i,right,m); %update the boundaries.
[y]=quicksort(y,left,j,m); %recursively call the function.
(1)請修復您的縮進。 (2)擴展你正在被卡住的地方(3)爲什麼你不讓你的遞歸函數只接受一個向量,然後做一個包裝函數,從矩陣'y'中提取行'm'並將其傳遞給你的遞歸函數。 – Dan
@Dan我會假設他們試圖根據行'm'排序整個矩陣。 @YYCCC我不認爲你的最後一個'return'是正確的。 – beaker