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"


發佈了15 篇原創文章 · 獲贊 7 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章