快速排序 shell实现

#!/bin/bash


a=(9 8 7 6 5 4 3 2 1 0 23)


function get_pos()
{
        local low=($1)
        local high=($2)
        local value=(${a[$low]})


        while [ $low -lt $high ]
        do
                while [ $low -lt $high -a $value -le ${a[$high]} ]
                do
                        high=$[$high-1]
                done
                a[$low]=${a[$high]}


                while [ $low -lt $high -a $value -ge ${a[$low]} ]
                do
                        low=$[$low+1]
                done
                a[$high]=${a[$low]}
        done

        a[$low]=$value

        return $low
}

function quick_sort()
{
        local low=($1)
        local high=($2)
        local pos=(0)
        local tempLow=(0)
        local tempHigh=(0)

        if [ $low  -lt $high ]
        then
                get_pos $low $high
                pos=($?)
                tempLow=$[$pos-1]
                tempHigh=$[$pos+1]

                quick_sort $low $tempLow
                quick_sort $tempHigh $high
        fi
}

quick_sort  0 10
echo ${a[@]}

 

 

 

你可能感兴趣的:(算法)