bash编程实现插入排序
2015-09-10 16:52
453 查看
插入排序是先把左边的数当成已排序列,然后逐个按大小插入数字至全部排列好的排序方法,下面是我参考百度百科C语言版改写的bash脚本。
#! /bin/bash
#插入排序
declare -i i
declare -i j
read -p "enter num: " -a num
i=1
while [ $i -lt ${#num[@]} ]
do
tm=${num[$i]}
j=$i-1
while [ $j -ge 0 ] && [ ${num[$j]} -gt $tm ]
do
num[$j+1]=${num[$j]}
j=$j-1
done
[ j != $i-1 ] && num[$j+1]=$tm
echo " ${num[@]}"
i=$i+1
done
ubuntu15.04终端测试结果
5 4 3 2 1
4 5 3 2 1
3 4 5 2 1
2 3 4 5 1
1 2 3 4 5
#! /bin/bash
#插入排序
declare -i i
declare -i j
read -p "enter num: " -a num
i=1
while [ $i -lt ${#num[@]} ]
do
tm=${num[$i]}
j=$i-1
while [ $j -ge 0 ] && [ ${num[$j]} -gt $tm ]
do
num[$j+1]=${num[$j]}
j=$j-1
done
[ j != $i-1 ] && num[$j+1]=$tm
echo " ${num[@]}"
i=$i+1
done
ubuntu15.04终端测试结果
5 4 3 2 1
4 5 3 2 1
3 4 5 2 1
2 3 4 5 1
1 2 3 4 5
相关文章推荐
- 插入排序
- 一波实用的Bash Shell整理
- php实现插入排序
- bash脚本输入密码不回显问题的解决方法
- bash 编程中循环语句用法
- Linux BASH多进程并行处理的方法实现
- BASH 学习笔记小结
- Java数据结构及算法实例:插入排序 Insertion Sort
- 写出健壮Bash Shell脚本的一些技巧总结
- Bash Shell中忽略大小写的设置方法
- Linux bash Shell中的变量类型详解
- linux BASH shell下设置字体及背景颜色
- bash获取当前路径示例
- Bash Shell 注释多行的几种方法
- linux bash字符串处理大全
- 让代码整洁、过程清晰的BASH Shell编程技巧
- 使用Bash Shell获取文件名和目录名的简单方法
- Bash Shell脚本学习小结
- bash批量修改文件名称的方法小结(增加,去除,修改后缀)
- Bash Shell中Shift用法分享