快速业务通道

linux bash shell实现对数组快速排序(升序)

作者 佚名技术 来源 Linux系统 浏览 发布时间 2012-03-24

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. local array=${3}
  8. eval local pivot=\$\{${array}[${1}]\}
  9. local low=${1}
  10. local high=${2}
  11. [ ${1} -ge ${2} ] && return
  12. while [ ${low} -lt ${high} ]; do
  13. while [ ${low} -lt ${high} -a ${pivot} -le $(eval echo \$\{${array}[${high}]\}) ]; do
  14. let high--
  15. done
  16. if [ ${pivot} -gt $(eval echo \$\{${array}[${high}]\}) ]; then
  17. eval ${array}[${low}]=\$\{${array}[${high}]\}
  18. eval ${array}[${high}]=${pivot}
  19. let low
  20. fi
  21. while [ ${low} -lt ${high} -a ${pivot} -ge $(eval echo \$\{${array}[${low}]\}) ]; do
  22. let low
  23. done
  24. if [ ${pivot} -lt $(eval echo \$\{${array}[${low}]\}) ]; then
  25. eval ${array}[${high}]=\$\{${array}[${low}]\}
  26. eval ${array}[${low}]=${pivot}
  27. let high--
  28. fi
  29. done
  30. #Execute the Quick_Sort function recursively
  31. Quick_Sort ${1} $[${low}-1] ${array}
  32. Quick_Sort $[${low} 1] ${2} ${array}
  33. unset array pivot low high
  34. }

2. shell脚本形式,进行简单测试(已将其放在附件里,文件名为chris-algo.sh.没法上传.sh脚本,故压缩打包了一下,文件名为:chris.zip).

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

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号