How do I sort an array in descending order without using the sort function?

 채택된 답변

You can use bubble sort:
swapped = 1;
while swapped
swapped = 0;
for i=1:numel(A)-1
if A(i+1) > A(i)
temp = A(i); A(i) = A(i+1); A(i+1) = temp; swapped = 1;
end
end
end

추가 답변 (2개)

Stephen23
Stephen23 2015년 11월 5일
편집: Stephen23 2015년 11월 5일
Here are a few ways to sort an array of unique values without using sort:
sortrows(A(:),-1)
A(end:-1:1) = unique(A);
fliplr(union(A,A(1)))
fliplr(setdiff(A,NaN))

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

질문:

2015년 11월 5일

답변:

2015년 11월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by