Sorting Arrays

  • Follow


Hi everyone,
my problem is I basically need to sort arrays without using the sort function.  I decided I should use insertion sort method.  Here is the code I have come up with so far:


function new=insertionsort(orig)
i=1; 
sz=length(orig);
while i <=sz
new=insert(new,orig(i,1));
i=i+1;
end

function orig=insert(orig,v)
i=1;
sz=length(orig);
done=false;
while i<=sz
if lt(v,orig(i,1))
done=true;
orig=[orig(1:i-1);v;orig(i:end)];
break;
end
i=i+1
end
if~done
orig(sz+1,1)=v;
end


I understand that I need to basically have two arrays, the original one that I use min to find the minimum value and move it to the new array.  The problem is that I'm not sure how to do this.  Any guidance or assistance would be greatly appreciated.
0
Reply kkemmerich (1) 12/15/2010 12:37:50 PM

matlabn00b <kkemmerich@hotmail.com> wrote in message <1998266770.62444.1292434700329.JavaMail.root@gallium.mathforum.org>...
> Hi everyone,
> my problem is I basically need to sort arrays without using the sort function.  I decided I should use insertion sort method.  Here is the code I have come up with so far:
> 
> 
> function new=insertionsort(orig)
> i=1; 
> sz=length(orig);
> while i <=sz
> new=insert(new,orig(i,1));
> i=i+1;
> end
> 
> function orig=insert(orig,v)
> i=1;
> sz=length(orig);
> done=false;
> while i<=sz
> if lt(v,orig(i,1))
> done=true;
> orig=[orig(1:i-1);v;orig(i:end)];
> break;
> end
> i=i+1
> end
> if~done
> orig(sz+1,1)=v;
> end
> 
> 
> I understand that I need to basically have two arrays, the original one that I use min to find the minimum value and move it to the new array.  The problem is that I'm not sure how to do this.  Any guidance or assistance would be greatly appreciated.

Seems like an awful lot of extra work to me.
You know the sizes of the two matrices so you should use for-loop rather than a while loop.  Then extract the minimum value one at a time.

%%%
A = rand(1,10);
B = zeros(size(A));
for ii = 1:length(B)
    [B(ii) idx] = min(A);
    A(idx) = [];
end
issorted(B)
%SCd
%%%
0
Reply Sean 12/15/2010 5:59:07 PM


 
> %%%
> A = rand(1,10);
> B = zeros(size(A));
> for ii = 1:length(B)
>     [B(ii) idx] = min(A);
>     A(idx) = [];
> end
> issorted(B)
> %SCd
> %%%

And for speed, instead of removing the minimum element, set it to the max.
A = rand(1,1000);
B = zeros(size(A));
mx = max(A);
for ii = 1:length(B)
    [B(ii) idx] = min(A);
    A(idx) = mx;
end
issorted(B)
0
Reply Sean 12/15/2010 6:22:05 PM

Thank you guys so much for the help.  If I wanted to input two arrays of equal length and sort it by the first array using the previous method and keep the "pairs" the same how would that work?  Any hints or help?  Thanks again.
EX:
array1=[4,2,6,1,3] %orig array before sort
array2=[2,4,7,1,5] %the "paired" array
newarray1=[1,2,3,4,6] %orig array just sorted
newarray2=[1,4,5,2,7] %second array sorted according to
%the first array
0
Reply matlabn00b 12/15/2010 7:44:37 PM

If you really wrote the code shown in your first post this second question should be a piece of cake.
0
Reply Matt 12/16/2010 1:56:05 AM

4 Replies
226 Views

(page loaded in 0.063 seconds)

Similiar Articles:













7/30/2012 8:51:41 AM


Reply: