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
分享!