Created
December 13, 2015 11:28
-
-
Save oktomus/5ebf6ae4d8aabf1c2ad8 to your computer and use it in GitHub Desktop.
Sort an array while inserting by ascending order in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
read N | |
read tab[0] | |
for (( i=1; i<N; i++ )); do | |
read x | |
j=0 | |
k=$i | |
while [ $k -gt $j ] | |
do | |
m=$((($j+$k)/2)) | |
if [ $x -lt ${tab[$m]} ]; then | |
k=$m | |
else | |
j=$(($m+1)) | |
fi | |
done | |
tab=( "${tab[@]:0:j}" $x "${tab[@]:j}") | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment