Sorting algorithm in matlab part 1 ( Thuật toán sắp xếp trong Matlab phần 1)
clc
clear all
%% INPUTS
% The vector of numbers
disp ('INPUTS')
disp('Input the vector of numbers')
A=[18 7 6 15 4 13];
disp(A)
%% SOLUTIONdisp(A)
% Number of entries, n
n=length(A);
% making (n-1) passes
for j=1:1:n-1
% comparing each number with the next and swapping
for i=1:1:n-1
if A(i)>A(i+1);
% temp is a variable where the numbers are kept
% temporarily for the switch
temp=A(i);
A(i)=A(i+1);
A(i+1)=temp;
end
end
end
%% OUTPUT
disp(' ')
disp ('OUTPUT')
disp ('The ascending matrix is')
xem chi tiết hơn tại đây : xem chi tiết
No comments:
Post a Comment