选择排序的Matlab版实现

tech2026-01-27  10

function [outputArg1,outputArg2] = SelectionSort(inputArg1,inputArg2) %SELECTIONSORT 此处显示有关此函数的摘要 %   此处显示详细说明 % 首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置。

% 再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。

% 重复第二步,直到所有元素均排序完毕。 sortarray=[3,5,1,-1,-7,4,9,-6,8,10,4]; [m,n]=size(sortarray); for i=1:n-1     minindex=i;     for j=minindex+1:n         if sortarray(:,j)<sortarray(:,minindex)             minindex=j;         end     end     temp=sortarray(:,i);     sortarray(:,i)=sortarray(:,minindex);     sortarray(:,minindex)=temp; end end

最新回复(0)