Advertisement
Shailrshah

Bubble Sort

Aug 29th, 2014
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.37 KB | None | 0 0
  1. #!/bin/bash
  2. printArray(){
  3.     echo ${a[*]}
  4. }
  5. exchange(){
  6.     temp=${a[$1]}
  7.     a[$1]=${a[$2]}
  8.     a[$2]=$temp
  9. }
  10. bubbleSort(){
  11.     for (( i=0; i<$n; i++ ))
  12.     do
  13.         for(( j=$i; j<$n; j++ ))
  14.         do
  15.           if [ ${a[i]} -gt ${a[j]} ]
  16.             then exchange $i $j
  17.           fi
  18.         done
  19.     done
  20. }
  21. echo "Enter the numbers to be sorted:-"
  22. read -a a
  23. n=${#a[@]}
  24. bubbleSort
  25. printArray
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement