how to sort in increasing order using loop?

for i=1:6
A(i)=input('')
end
for i=1:6
for j=1:5
if A(j)>=A(j+1)
B=A(j);
A(j)=A(j+1);
A(j+1)=B;
end
end
end

6 Kommentare

David Goodmanson
David Goodmanson am 16 Dez. 2017
Hello baiti, what don't you like about this? It's a bubble sort in increasing order and it works.
baiti
baiti am 16 Dez. 2017
then, i think my matlab is the problem. i really dont have any idea why it wont work on my matlab. any suggestion?
David Goodmanson
David Goodmanson am 16 Dez. 2017
It's cleaner to put A = zeros(1,6) at the beginning, so that if you run the script more than once you get a fresh start for A. I put disp(A) at the end as well, so that you can see the results of your efforts.
Image Analyst
Image Analyst am 16 Dez. 2017
To format your code read this
Why don't you just use the built-in sort() function?
Jan
Jan am 17 Dez. 2017
@baiti: It seems like you have a problem running this code. Then it would be useful, if you mention what the problem is. "it wont work on my matlab" does not allow to reconsider, why you are not satisfied.
Manish Kumar
Manish Kumar am 26 Jun. 2020
Do you want better algorithm than this?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

rohan hazarika
rohan hazarika am 18 Sep. 2020

0 Stimmen

for i=1:6
A(i)=input('')
end
for i=1:6
for j=1:5
if A(j)>=A(j+1)
B=A(j);
A(j)=A(j+1);
A(j+1)=B;
end
end
end

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 16 Dez. 2017

Beantwortet:

am 18 Sep. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by