bubble sort program

https://wenku.baidu.com/view/34f7f9c608a1284ac8504324.html

 aa(:,1)=[6 5 4 7 8 3 2 9];
 aa(:,2)=[9 8 7 6 5 4 3 2];

a=aa(:,1);
b=aa(:,2);

N=length(a);
for i=1:N
swap=0;
for j=1:Ni
if a(j)<a(j+1)
temp=a(j);
a(j)=a(j +1);
a(j+1)=temp; % sort the first column

tempb=b(j);
b(j)=b(j+1);
b(j+1)=tempb;% The second column is sorted according to the first column

swap=1; %swap function: when the subsequent data has been sorted in order, it will not continue
end
end

if(~swap)
break;
end
end
result(:,1)=a(:,1 );
result(:,2)=b(:,1);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324652746&siteId=291194637