bubble sort function that sort element of a vector from tha largest to the smallest

7 Ansichten (letzte 30 Tage)
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

Antworten (2)

Walter Roberson
Walter Roberson am 9 Dez. 2012
You never set t to 0 so your loop never exits.
  4 Kommentare
kevin piaget
kevin piaget am 9 Dez. 2012
Bearbeitet: Walter Roberson am 10 Dez. 2012
i made this chance but i still don't have an answer
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==0;
for i=1:(n-1)
if x(i)>x(i+1)
x(i+1)=x(i);
swap(x(i+1),x(i))
t=1;
end
end
end
Walter Roberson
Walter Roberson am 10 Dez. 2012
I cannot seem to locate any Mathworks function named "swap" ?

Melden Sie sich an, um zu kommentieren.


JayaSaiRamPavan Tanneru
JayaSaiRamPavan Tanneru am 22 Feb. 2020
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);

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by