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

 採用された回答

Thorsten
Thorsten 2015 年 11 月 5 日
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 ExchangeShifting 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