asmodeus94

sortowanieBabelkowe

Dec 5th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.94 KB | None | 0 0
  1. #!/bin/bash
  2. function zamien {
  3.         swaper=${tab[$1]}
  4.         tab[$1]=${tab[$1+1]}
  5.         tab[$1+1]=$swaper
  6. }
  7. read -p "Podawaj liozby: " -a tab
  8. read -p "Sortowanie rosnace czy malejace? 1 - rosnace, 2 - malejace: " rosCzyMal
  9. echo Przed sortowaniem: ${tab[*]}
  10. export tab
  11. ilEle=$[${#tab[*]}-1]
  12. for(( y=0 ; y<ilEle ; y++ ))
  13. do
  14.         for(( x=0 ; x<$[ilEle-y] ; x++ ))
  15.         do
  16.                 if [ $rosCzyMal -eq 1 ]
  17.                 then
  18.                         if [ ${tab[x]} -gt ${tab[x+1]} ]
  19.                         then
  20.                                 zamien $x
  21.                         fi
  22.                         elif [ $rosCzyMal -eq 2 ]
  23.                         then
  24.                                 if [ ${tab[x]} -lt ${tab[x+1]} ]
  25.                                 then
  26.                                         zamien $x
  27.                                 fi
  28.                 fi
  29.         done
  30. done
  31. echo Po sortowaniu: ${tab[*]}
Advertisement
Add Comment
Please, Sign In to add comment