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

    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

他的chris-algo.sh文件

#!/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}
echo $pivot
    [ ${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



我自己的shell实现

#!/bin/bash

q_sort()
{
	local array=${3} 
   	local left=${1}
	local right=${2}
	local i=${1}
	local j=${2}
	local mid
	let mid=(left+right)/2
    	eval local midValue=\$\{${array}[${mid}]\}

	[ ${left} -gt ${right} ] && return

	while [ ${i} -le ${j} ]
	do
		while [ ${i} -lt ${right} -a ${midValue} -lt $(eval echo \$\{${array}[${i}]\}) ]; do
			let i++;done
		while [ ${j} -gt ${left} -a ${midValue} -gt $(eval echo \$\{${array}[${j}]\}) ]; do
			let j--;done
		
		if [ ${i} -le ${j} ]; then
			temp=$(eval echo \$\{${array}[${i}]\})
			eval ${array}[${i}]=\$\{${array}[${j}]\}
			eval ${array}[${j}]=$temp
			let i++
			let j--
			#echo "daozheli"
		fi
	done

	if [ ${i} -lt ${right} ];then
		q_sort ${i} ${right} ${array}
	fi
	if [ ${j} -gt ${left} ];then
		q_sort ${left} ${j} ${array}
	fi 

echo $(eval echo \$\{${array}[*]\})
}

arr=(2 5 6 8 8 3 4 345 1356 21)
for((i=0;i<10;i++)); do
        printf "%d " ${arr[${i}]}
    done

q_sort 0 9 arr
for((i=0;i<10;i++)); do
        printf "%d " ${arr[${i}]}	
    done
printf "\n"


你可能感兴趣的:(Linux)