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

2 Ansichten (letzte 30 Tage)
A = [2 1 4 3 6 5 8 7 10 9 12 11 14 13 16 15 18 17 20 19]

Akzeptierte Antwort

Thorsten
Thorsten am 5 Nov. 2015
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

Weitere Antworten (2)

Walter Roberson
Walter Roberson am 5 Nov. 2015
flipud(unique(A(:)))

Stephen23
Stephen23 am 5 Nov. 2015
Bearbeitet: Stephen23 am 5 Nov. 2015
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))

Kategorien

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by