Skip to content

Instantly share code, notes, and snippets.

@oktomus
Created December 13, 2015 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oktomus/5ebf6ae4d8aabf1c2ad8 to your computer and use it in GitHub Desktop.
Save oktomus/5ebf6ae4d8aabf1c2ad8 to your computer and use it in GitHub Desktop.
Sort an array while inserting by ascending order in bash
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