linux bash shell實現對數組快速排序(升序)

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):

  1. Quick_Sort(){  
  2.     #Sort Numeric-array in ASC order, using normal Quick-Sort algorithm.  
  3.     #C code: http://www.cnblogs.com/skyaspnet/archive/2010/11/03/1868298.html  
  4.     #Usage: Quick_Sort lowest_index highest_index array_name  
  5.     #e.g.,  Quick_Sort 0 9 array1  
  6.     #e.g.,  Quick_Sort 1 3 array2  
  7.  
  8.     local array=${3}  
  9.     eval local pivot=\$\{${array}[${1}]\}  
  10.     local low=${1}  
  11.     local high=${2}  
  12.  
  13.     [ ${1} -ge ${2} ] && return 
  14.  
  15.     while [ ${low} -lt ${high} ]; do  
  16.         while [ ${low} -lt ${high} -a ${pivot} -le $(eval echo \$\{${array}[${high}]\}) ]; do  
  17.             let high--  
  18.         done  
  19.         if [ ${pivot} -gt $(eval echo \$\{${array}[${high}]\}) ]; then 
  20.             eval ${array}[${low}]=\$\{${array}[${high}]\}  
  21.             eval ${array}[${high}]=${pivot}  
  22.             let low++  
  23.         fi  
  24.  
  25.         while [ ${low} -lt ${high} -a ${pivot} -ge $(eval echo \$\{${array}[${low}]\}) ]; do  
  26.             let low++  
  27.         done  
  28.         if [ ${pivot} -lt $(eval echo \$\{${array}[${low}]\}) ]; then 
  29.             eval ${array}[${high}]=\$\{${array}[${low}]\}  
  30.             eval ${array}[${low}]=${pivot}  
  31.             let high--  
  32.         fi  
  33.     done  
  34.  
  35.     #Execute the Quick_Sort function recursively  
  36.     Quick_Sort ${1} $[${low}-1] ${array}  
  37.     Quick_Sort $[${low}+1] ${2} ${array}  
  38.  
  39.     unset array pivot low high  
  40. }  

 

 

2. shell腳本形式,進行簡單測試(已將其放在附件裏,文件名爲chris-algo.sh。由於沒法上傳.sh腳本,故壓縮打包了一下,文件名爲:chris.zip)。

  1. #!/bin/bash   
  2. ##################################################   
  3. ## Author     :  Chris   
  4. ## Create Date:  2011-12-19   
  5. ## Modify Date:  2012-05-14  
  6. ## Realize common algorithms in bash-v3.x   
  7. ## Note: Every function represents an algorithm.   
  8. ##################################################   
  9.   
  10. #Normal Quick-Sort algorithm   
  11. Quick_Sort(){   
  12.     #Sort Numeric-array in ASC order, using normal Quick-Sort algorithm.   
  13.     #C code: http://www.cnblogs.com/skyaspnet/archive/2010/11/03/1868298.html   
  14.     #Usage: Quick_Sort lowest_index highest_index array_name   
  15.     #e.g.,  Quick_Sort 0 9 array1   
  16.     #e.g.,  Quick_Sort 1 3 array2   
  17.   
  18.     local array=${3}   
  19.     eval local pivot=\$\{${array}[${1}]\}   
  20.     local low=${1}   
  21.     local high=${2}   
  22.   
  23.     [ ${1} -ge ${2} ] && return  
  24.   
  25.     while [ ${low} -lt ${high} ]; do   
  26.         while [ ${low} -lt ${high} -a ${pivot} -le $(eval echo \$\{${array}[${high}]\}) ]; do   
  27.             let high--   
  28.         done   
  29.         if [ ${pivot} -gt $(eval echo \$\{${array}[${high}]\}) ]; then  
  30.             eval ${array}[${low}]=\$\{${array}[${high}]\}   
  31.             eval ${array}[${high}]=${pivot}   
  32.             let low++   
  33.         fi   
  34.   
  35.         while [ ${low} -lt ${high} -a ${pivot} -ge $(eval echo \$\{${array}[${low}]\}) ]; do   
  36.             let low++   
  37.         done   
  38.         if [ ${pivot} -lt $(eval echo \$\{${array}[${low}]\}) ]; then  
  39.             eval ${array}[${high}]=\$\{${array}[${low}]\}   
  40.             eval ${array}[${low}]=${pivot}   
  41.             let high--   
  42.         fi   
  43.     done   
  44.   
  45.     #Execute the Quick_Sort function recursively   
  46.     Quick_Sort ${1} $[${low}-1] ${array}   
  47.     Quick_Sort $[${low}+1] ${2} ${array}   
  48.   
  49.     unset array pivot low high   
  50. }   
  51.   
  52. main(){   
  53.     read -ep "Input Numeric: " numeric 
  54.     size=$(echo ${numeric} | awk '{print NF}'
  55.  
  56.     #Define array   
  57.     t_array=(${numeric}) 
  58.   
  59.     #Output the original array   
  60.     for((i=0;i<${size};i++)); do   
  61.         printf "%d " ${t_array[${i}]}   
  62.     done   
  63.     printf "\n"  
  64.   
  65.     #Using Quick_Sort function to sort t_array   
  66.     size_1=$[${size} - 1] 
  67.     Quick_Sort 0 ${size_1} t_array   
  68.   
  69.     #Output the sorted array   
  70.     for((i=0;i<${size};i++)); do   
  71.         printf "%d " ${t_array[${i}]}   
  72.     done   
  73.     printf "\n"  
  74. }   
  75.   
  76. 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

 

分享!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章