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: Sorting Arrays - comp.soft-sys.matlabHi 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 t... Sort cell array - comp.soft-sys.matlabHi, I would like to sort a cell array which contains names with letters and numbers. Here is a example : X = {'S1', 'H2', 'S3', 'H4', 'B6', .... awk challenge: sort array using only for ... in ... - comp.lang ...This occurred to me as an interesting challenge in awk: Given an array, output its indices and corresponding elements sorted by element value using O... How to sort array - comp.lang.awkHi All How to using sort in awk ? try printf("%s\n",a[ele]) | "sort" is not good. #!/bin/ksh # 2008/12/11 echo "" | awk '{ a[2] = "d 1" ... alphabetical sort of cell array - comp.soft-sys.matlabi'm trying to sort a 2 column cell array ('test') using the test2 = sortrows(test,2) function, but all it does is spit out the original, unsorted arra... Sorting Strings and uniquifying - comp.soft-sys.matlabI have a bunch of image, for each image I want to associate a subject string. Forming a cell array or an array of strings -- not a problem. Then I want to sort the ... sorting even and odd numbers - comp.databases.filemakerSort cell array - comp.soft-sys.matlab Hi, I would like to sort a cell array which contains names with letters and numbers. ... Updated 05 Mar 2008) This function will ... A Sorting Circuit in Digital Logic Design - comp.lang.vhdl ...Hello; I am trying to implement a Linear Array Sort that takes in 10 8-bit inputs serially and outputs the inputs in increasing order. I figured out ... how to display the number entered in descending orderb - comp.lang ...Later, when you > have to display the numbers in descending order, you'll have to do more > complex stuff: probably involving an array and the Arrays.sort standard > API ... VHDL Design for running sorter - comp.arch.fpgaIn every clock cyle I have to sort 32-size array. When I have sorted the array in ascending order, I want to choose 24th number only. In the next clock cycle, I get a new ... Arrays in C++ - Sorting - Math Bits High School Math ResourcesArrays in C++ - Sorting. ... Sorting Arrays * Definition: Sorting is the process of putting data in order; Java: Sorting ArraysJava Notes Sorting Arrays Why you shouldn't write your own sort. Good student problem. A favorite computer science topic it sorting, putting a collection of data in ... 7/30/2012 8:51:41 AM
|