[用MATLAB写算法]之排序算法1)插入排序
2017-3-13 13:09
阅读:6865
简单的插入排序,算法复杂度为Θ(n2).
filename: insertion_sort
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function sorted_array=insertion_sort(num_array)
% sorted_array=insertion_sort(num_array) ascending
% algorithm complexity: theta(n^2)
for i=2:length(num_array)
for j=1:i-1
if num_array(i)<num_array(j)
tmp=num_array(i);
for k=i-1:-1:j
num_array(k+1)=num_array(k);
end
num_array(j)=tmp;
break
end
end
end
sorted_array=num_array;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
运行示例:
转载本文请联系原作者获取授权,同时请注明本文来自徐勇刚科学网博客。
链接地址:https://wap.sciencenet.cn/blog-71294-1039179.html?mobile=1
收藏
当前推荐数:1
推荐人:
推荐到博客首页
网友评论0 条评论