This video shows the implementation of the Selection Sort algorithm using MATLAB Function.
We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com
Source Code:
function y = SelectionSort(x)
n = length(x);
for i=1:n
min_index = i;
for j=i+1:n
if x(min_index) greater_than x(j)
min_index=j;
end
end
%Swap
if(min_index~=i)
temp = x(min_index);
x(min_index) = x(i);
x(i) = temp;
end
end
y=x;
end
Q:3 Write the Matlab code which will give the number of element greater than 5 in the Array A= [ 1 -6 12 4 6 8 9 2] and it will also specify the location of these elements.
I think this will help:
A(A>5)
or assign it to a variable:
B = A(A>5)
Cheers
Programmer World
–
this program make the order from small to big but how to do this from big to small?
Just change the comparison from greater_than to less_than. The modified function will be as below:
function y = SelectionSort(x)
n = length(x);
for i=1:n
max_index = i;
for j=i+1:n
if x(max_index) less_than x(j)
max_index=j;
end
end
%Swap
if(max_index~=i)
temp = x(max_index);
x(max_index) = x(i);
x(i) = temp;
end
end
y=x;
end
It should work.
Cheers
Programmer World
–