matlab descending order algorithm without sort function

조회 수: 7 (최근 30일)
DAIN LEE
DAIN LEE 2017년 10월 31일
댓글: Bjorn Gustavsson 2020년 3월 10일
i want to know how to descending order (1Xn) matrix without sort function.
for example A=[1 3 6 9 8 5 10 2]
  댓글 수: 2
Bjorn Gustavsson
Bjorn Gustavsson 2017년 10월 31일
Why not use sort when someone paid to give you a software that has that function? Homework? If homework just look up any of the algorithms for sorting and implement (one of) them...
Andrei Bobrov
Andrei Bobrov 2017년 10월 31일
homework?
Please read here, point 4.

댓글을 달려면 로그인하십시오.

채택된 답변

Birdman
Birdman 2017년 10월 31일
A=[1 3 6 9 8 5 10 2];temp=0;
for i=1:j
for j=1:length(A)
if(A(i)>A(j))
temp=A(i);
A(i)=A(j);
A(j)=temp;
end
end
end
disp(A)
This is bubble sorting algorithm.
  댓글 수: 2
Maria zhou
Maria zhou 2020년 3월 9일
hello,..
could you explain this code?? i have home work and this code works
Bjorn Gustavsson
Bjorn Gustavsson 2020년 3월 10일
Google bubble-sort. Look for bubble-sort videos on youtube. Look for bubble-sort on wikipedia. Run the script in debug-mode and look at how A changes step by step. We clearly understand this is a home-work. However, the idea with homework is that you should learn something from it. Read the link Andrei Bobrov gave. Check the link KSSV gave.
To run the scrip in debug-mode save the code snippet in a file, perhaps my_bubble_sort.m
then at the matlab command-line promp type:
>> dbstop in my_bubble_sort
>> my_bubble_sort
Then you run the sorting step-by-step and look at how your variable A changes betwee each step.
The common theme in each of the points here is: "you do"
HTH

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

KSSV
KSSV 2017년 10월 31일

Rediet Tesfu
Rediet Tesfu 2019년 9월 27일
How do i print out vector in order

카테고리

Help CenterFile Exchange에서 디버그 및 분석에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!