2011年12月19日,参考网上用C语言实现的快速排序,经过一番修改后,用shell(我的测试环境为centos5的bash-v3.x)实现了相同功能:对数组进行升序排序。
注:如果代码框里的代码复制出来后显示异常,就麻烦下载附件chris.zip(已将chris-qsort.sh和chris-algo.sh压缩打包为chris.zip)
1. shell函数形式(已将其放在附件里,文件名为:chris-qsort.sh。由于没法上传.sh脚本,故压缩打包了一下,文件名为:chris.zip):
- Quick_Sort(){
- #Sort Numeric-array in ASC order, using normal Quick-Sort algorithm.
- #C code: http://www.cnblogs.com/skyaspnet/archive/2010/11/03/1868298.html
- #Usage: Quick_Sort lowest_index highest_index array_name
- #e.g., Quick_Sort 0 9 array1
- #e.g., Quick_Sort 1 3 array2
- local array=${3}
- eval local pivot=\$\{${array}[${1}]\}
- local low=${1}
- local high=${2}
- [ ${1} -ge ${2} ] && return
- while [ ${low} -lt ${high} ]; do
- while [ ${low} -lt ${high} -a ${pivot} -le $(eval echo \$\{${array}[${high}]\}) ]; do
- let high--
- done
- if [ ${pivot} -gt $(eval echo \$\{${array}[${high}]\}) ]; then
- eval ${array}[${low}]=\$\{${array}[${high}]\}
- eval ${array}[${high}]=${pivot}
- let low++
- fi
- while [ ${low} -lt ${high} -a ${pivot} -ge $(eval echo \$\{${array}[${low}]\}) ]; do
- let low++
- done
- if [ ${pivot} -lt $(eval echo \$\{${array}[${low}]\}) ]; then
- eval ${array}[${high}]=\$\{${array}[${low}]\}
- eval ${array}[${low}]=${pivot}
- let high--
- fi
- done
- #Execute the Quick_Sort function recursively
- Quick_Sort ${1} $[${low}-1] ${array}
- Quick_Sort $[${low}+1] ${2} ${array}
- unset array pivot low high
- }
2. shell脚本形式,进行简单测试(已将其放在附件里,文件名为chris-algo.sh。由于没法上传.sh脚本,故压缩打包了一下,文件名为:chris.zip)。
- #!/bin/bash
- ##################################################
- ## Author : Chris
- ## Create Date: 2011-12-19
- ## Modify Date: 2012-05-14
- ## Realize common algorithms in bash-v3.x
- ## Note: Every function represents an algorithm.
- ##################################################
- #Normal Quick-Sort algorithm
- Quick_Sort(){
- #Sort Numeric-array in ASC order, using normal Quick-Sort algorithm.
- #C code: http://www.cnblogs.com/skyaspnet/archive/2010/11/03/1868298.html
- #Usage: Quick_Sort lowest_index highest_index array_name
- #e.g., Quick_Sort 0 9 array1
- #e.g., Quick_Sort 1 3 array2
- local array=${3}
- eval local pivot=\$\{${array}[${1}]\}
- local low=${1}
- local high=${2}
- [ ${1} -ge ${2} ] && return
- while [ ${low} -lt ${high} ]; do
- while [ ${low} -lt ${high} -a ${pivot} -le $(eval echo \$\{${array}[${high}]\}) ]; do
- let high--
- done
- if [ ${pivot} -gt $(eval echo \$\{${array}[${high}]\}) ]; then
- eval ${array}[${low}]=\$\{${array}[${high}]\}
- eval ${array}[${high}]=${pivot}
- let low++
- fi
- while [ ${low} -lt ${high} -a ${pivot} -ge $(eval echo \$\{${array}[${low}]\}) ]; do
- let low++
- done
- if [ ${pivot} -lt $(eval echo \$\{${array}[${low}]\}) ]; then
- eval ${array}[${high}]=\$\{${array}[${low}]\}
- eval ${array}[${low}]=${pivot}
- let high--
- fi
- done
- #Execute the Quick_Sort function recursively
- Quick_Sort ${1} $[${low}-1] ${array}
- Quick_Sort $[${low}+1] ${2} ${array}
- unset array pivot low high
- }
- main(){
- read -ep "Input Numeric: " numeric
- size=$(echo ${numeric} | awk '{print NF}')
- #Define array
- t_array=(${numeric})
- #Output the original array
- for((i=0;i<${size};i++)); do
- printf "%d " ${t_array[${i}]}
- done
- printf "\n"
- #Using Quick_Sort function to sort t_array
- size_1=$[${size} - 1]
- Quick_Sort 0 ${size_1} t_array
- #Output the sorted array
- for((i=0;i<${size};i++)); do
- printf "%d " ${t_array[${i}]}
- done
- printf "\n"
- }
- main
输出如下:
[root@localhost algorithms]# ./chris-algo.sh
49 38 65 97 76 13 27 9 2 1
1 2 9 13 27 38 49 65 76 97
分享!
转载于:https://blog.51cto.com/cyent/745659