How can i find the sorted indexing of the array
1 次查看(过去 30 天)
显示 更早的评论
Hi, Sir how can i find the sorted indexing of the array when i am sorting it in the same code??
A = [2 9 6 5 8]
swapped = 1;
while swapped
swapped = 0;
for i=1:numel(A)-1
if A(i+1) > A(i)
memory = A(i);
A(i) = A(i+1);
A(i+1) = memory;
swapped = 1;
end
end
end
disp(A)
0 个评论
采纳的回答
Andrei Bobrov
2019-5-6
A = [2 9 6 5 8];
n = numel(A);
AA = [A;1:n];
swapped = 1;
while swapped
swapped = 0;
for ii = 1:n-1
if AA(1,ii+1) > AA(1,ii)
memory = AA(:,ii);
AA(:,ii) = AA(:,ii+1);
AA(:,ii+1) = memory;
swapped = 1;
end
end
end
Asort = AA(1,:);
Index_sort = AA(2,:);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!