• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

MATLAB实现搬运的排序 【例】

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
带有搬运的排序。对数组arr1 进行升序排序,与arr1 中相对应的arr2 中的元素也
要发生改变。对这个种排序,每次arr1 中的一个元素与另一个元素进行交换,arr2 中对应的
元素也要进行相应的交换。当排序结束时arr1 中的元素按升序排列,arr2 中的元素也会有相
应的变化。
当arr1 的数组排序结束后,arr2 也要进行相应的变化。两数组为
编写一个程序,对第一个实数组进行按降序排列,对第二个数组进行相应交化。用下面
两个数组检测你的程序
a = [-1, 11, -6, 17, -23, 0, 5, 1, -1];
b = [-31, 102, 36, -17, 0, 10, -8, -1, -1];


1.ssort00.m

function [out1,out2] = ssort00(a,b)
%SSORT Selection sort data in ascending order
% Function SSORT sorts a numeric data set into
% ascending order. Note that the selection sort
% is relatively inefficient. DO NOT USE THIS
% FUNCTION FOR LARGE DATA SETS. Use MATLAB\'s
% "sort" function instead.
% Define variables:
% a --Input array to sort
% ii --Index variable
% iptr --Pointer to min value
% jj --Index variable
% nvals --Number of values in "a"
% out --Sorted output array
% temp --Temp variable for swapping
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 12/19/98 S. J. Chapman Original code
% Get the length of the array to sort
nvals = size(a,2);
% Sort the input array
for ii = 1:nvals-1
% Find the minimum value in a(ii) through a(n)
iptr = ii;
for jj = ii+1:nvals
if a(jj) < a(iptr)
iptr = jj;
end
end
% iptr now points to the minimum value, so swap a(iptr)
% with a(ii) if ii ~= iptr.
if ii ~= iptr
temp1 = a(ii);
temp2=b(ii);
a(ii) = a(iptr);
b(ii)=b(iptr);
a(iptr) = temp1;
b(iptr)=temp2;
end
end
% Pass data back to caller
out1 = a;
out2 = b;


2.test.m
clc
clear all
nvals = input(\'Enter number of values to sort: \');
% Preallocate array
array1 = zeros(1,nvals);
array2 = zeros(1,nvals);
% Get input values
for i = 1:nvals
%Prompt for next value
string1 = [\'Enter a value \' int2str(i) \': \'];
array1(i)=input(string1);
end
for ii = 1:nvals
%Prompt for next value
string2 = [\'Enter b value \' int2str(ii) \': \'];
array2(ii)=input(string2);
end
% Now sort the data
[out1,out2]=ssort00(array1,array2);
% Display the sorted result.
fprintf(\'\nBefore sorted data:\n\');
for iii = 1:nvals
fprintf(\'%d  \',array1(iii));
end
fprintf(\'\n\');
for iiii = 1:nvals
fprintf(\'%d  \',array2(iiii));
end
fprintf(\'\nAfter sorted data:\n\');
for iii = 1:nvals
fprintf(\'%d  \',out1(iii));
end
fprintf(\'\n\');
for iiii = 1:nvals
fprintf(\'%d  \',out2(iiii));
end
fprintf(\'\n\');

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
delphi读取磁盘信息发布时间:2022-07-18
下一篇:
delphi数组如何初始化发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap