matlab快速排序

function [ A ] = quickSort( A,p,r )
%UNTITLED5 Summary of this function goes here
%   Detailed explanation goes here
if p<r
    [A,q]=partition(A,p,r);
    A=quickSort(A,p,q-1);
    A=quickSort(A,q+1,r);
end


end


function [ A,q ] = partition( A,p,r )
%UNTITLED6 Summary of this function goes here
%   Detailed explanation goes here
x=A(r);
i=p-1;
for j=p:r-1
    if A(j)<=x
        i=i+1;
        temp=A(j);
        A(j)=A(i);
        A(i)=temp;
    end
end
 temp=A(i+1);
 A(i+1)=A(r);
 A(r)=temp;
q=i+1;


end


阅读更多

没有更多推荐了,返回首页