hi this is my homework ı want to sort matrix A from smallest to largest.But I m stuck here .Can you help me ? Ps:I cant use sort,min,max or other functions
1 view (last 30 days)
Show older comments
clc;clear;
A=[24.3 24.8 24.6 24.0 22.3 21.0 20.5 20.7 18.8 17.6 15.5 17.5 19.0 19.8 21.4]
B=[]
for i=1:length(A)
ek=100;
for i=1:length(A)
if A(i)<ek
ek=A(1,i)
end
end
B(1,i)=A(i)
A(i)=[]
end
B
3 Comments
per isakson
on 5 Apr 2022
Edited: per isakson
on 5 Apr 2022
Google for "selection sort pseudocode". I think Selection sort pseudocode is an appropriate read. I'm sure that, regardless of the definition of functions, loops and if-statemens and logical operators are allowed.
Answers (2)
Walter Roberson
on 5 Apr 2022
It is not possible to solve that problem in MATLAB without using any functions. For an explaination see https://www.mathworks.com/matlabcentral/answers/38787-what-can-be-programmed-without-any-built-in-functions?s_tid=srchtitle
0 Comments
Walter Roberson
on 5 Apr 2022
clc;clear;
A=[24.3 24.8 24.6 24.0 22.3 21.0 20.5 20.7 18.8 17.6 15.5 17.5 19.0 19.8 21.4]
B=[]
for i=1:length(A)
ek=100;
j_at_min = 0;
for j=1:length(A)
if A(j)<ek
ek=A(1,j);
j_at_min = j;
end
end
B(1,i)=A(j_at_min)
A(j_at_min)=[]
end
B
0 Comments
See Also
Categories
Find more on Shifting and Sorting Matrices 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!