필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

I am trying to implement a selection sort function

조회 수: 2 (최근 30일)
Kevin Junior
Kevin Junior 2013년 10월 16일
마감: Walter Roberson 2013년 10월 19일
what I got is a little too messy I want a more concise code for selection sort
function [y,TotalC,TotalS] = selectSort(x)
% x is an n-vector
% y is x with x(1) and x(m) swapped where x(m) is the minimum value in x
n = length(x);
TotalC = 0;
TotalS = 0;
S = 0;
for i = (1:n-1)
m = y(i);
minIndex = i;
for j = (i:n)
if y(j) <= m
m = y(j);
minIndex = j;
end
end
if i ~= minIndex
y([minIndex i]) = y([i minIndex]); %Swap
end
TotalC = TotalC + C;
TotalS = TotalS + S;
end %for

답변 (0개)

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by