bubble sort function that sort element of a vector from tha largest to the smallest
18 次查看(过去 30 天)
显示 更早的评论
defined function that sorts the elements of a vector (of any length) from largest to smallest.
this is my work
function [ y ] = downsort_function( x )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
n=numel(x);
t=1;
a=0;
while t==1;
for i=1:(n-1)
if x(i)>x(i+1)
x(i+1)=x(i);
t=1;
end
end
end
the problem is when i try to read with my vector, my machine gets stuck. it not giving me anything or i have to restart the machine.
thanks for yr help
0 个评论
回答(2 个)
JayaSaiRamPavan Tanneru
2020-2-22
x=input('enter:');
k=length(x);
for n=1:k
for m=1:k
if x(n)>x(m)
temp=x(n);
x(n)=x(m);
x(m)=temp;
end
end
end
disp(x);
1 个评论
James Tursa
2020-2-24
Your m indexing could be better, and you have no short-circuit exit if there are no swaps.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!