Sorting Algorithm

51 views (last 30 days)
Buddy Britton
Buddy Britton on 4 May 2011
I need to take two arrays and sort one in ascending order, while carrying along the second one. I cannot use the sort function in Matlab.
ex) Array1 = [3,-1,-34,10,8] Array2 = [2,-9,4,-5,0]

Answers (4)

Sean de Wolski
Sean de Wolski on 4 May 2011
Then even better - trump your teacher with sortrows!
A = sortrows([array1(:),array2(:)]);
array1_sorted = A(:,1).';
array2_sorted = A(:,2).';

Andrei Bobrov
Andrei Bobrov on 5 May 2011
more variant
Array1 = [3,-1,-34,10,8];
Array2 = [2,-9,4,-5,0];
A = [Array1;Array2];
SortedArray = [];
while ~isempty(A)
[~,I] = min(A(1,:));
A(:,[1 I]) = A(:,[I 1]);
SortedArray = [SortedArray A(:,1)];
A = A(:,2:end);
end
or
Array1 = [3,-1,-34,10,8];
Array2 = [2,-9,4,-5,0];
A = [Array1;Array2];
k = 1:length(A);
while ~isempty(k)
[~,I] = min(A(1,k));
A(:,[k(1) k(I)]) = A(:,[k(I) k(1)]);
k = k(2:end);
end
or
Array1 = [3,-1,-34,10,8];
Array2 = [2,-9,4,-5,0];
A = [Array1;Array2];
n = length(A);
for j = 1:n
k = j:n;
[~,I] = min(A(1,k));
A(:,[j k(I)]) = A(:,[k(I) j]);
end
condition without built-in sort functions. I think sortrows, sort and unique - sort function MATLAB

Matt Tearle
Matt Tearle on 4 May 2011
In the spirit of Sean's answer... this works for the given example:
[~,idx] = unique(Array1);
Array2(idx)
EDIT (and may Cleve have mercy on my soul)
[s1,idx] = unique(Array1);
if numel(s1)<numel(Array1)
s2 = [];
for k=1:numel(s1)
s2 = [s2,Array2(s1(k)==Array1)];
end
else
s2 = Array2(idx);
end
s2
  7 Comments
Buddy Britton
Buddy Britton on 4 May 2011
Matt, I tried running your edited version and there is an error in line 1 "[s1,idx] = unique(Array1)"
Matt Tearle
Matt Tearle on 5 May 2011
a) Any particular error? Given that it works fine for me (R2011a), it's a bit hard for me to tell why you might be getting "an error".
b) I don't know why you can't use the sort function, but, whatever the reason, I doubt you can really use the unique function either. (Or sortrows)
b, part 2) Sean and I gave answers that technically didn't use sort, but are not good ways to solve the problem (unless there really is a reason you can use unique or sortrows, but not sort).

Sign in to comment.


Daniel Shub
Daniel Shub on 5 May 2011
I think the sortrows/unique answers may be cheating since they might call sort under the hood ... I am sure this student would not want to cheat. My solution uses both eval and feval, so should get extra credit.
Array1 = [3,-1,-34,10,8];
Array2 = [2,-9,4,-5,0];
while 42 > sqrt(pi)
eval('x = randperm(length(Array1));');
if feval('all', diff(Array1(x)) > 0)
return;
else
disp('MATLAB is stupid and guessed wrong!');
disp(['[', num2str(Array1(x)), '] is not sorted!']);
disp('Trying again!');
disp(' ');
end
end
Array1(x)
Array2(x)
Any concerns about inefficiencies are silly as the distributed computing toolbox would allow my answer to harness the power of a computer cluster.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!