Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- printArray(){
- echo ${a[*]}
- }
- exchange(){
- temp=${a[$1]}
- a[$1]=${a[$2]}
- a[$2]=$temp
- }
- bubbleSort(){
- for (( i=0; i<$n; i++ ))
- do
- for(( j=$i; j<$n; j++ ))
- do
- if [ ${a[i]} -gt ${a[j]} ]
- then exchange $i $j
- fi
- done
- done
- }
- binarySearch(){
- l=0
- ans=0
- u=$(($n-1))
- while [ $l -le $u ]
- do
- mid=$(((( $l+$u ))/2 ))
- if [ $s -eq ${a[$mid]} ]
- then
- ans=1
- break
- elif [ $s -lt ${a[$mid]} ]
- then u=$(($mid-1))
- else l=$(($mid+1))
- fi
- done
- if [ $ans -eq 1 ]
- then echo "Element found at position $(($mid+1))"
- else echo "Element not found"
- fi
- }
- echo "Enter the numbers to be sorted:-"
- read -a a
- n=${#a[@]}
- bubbleSort
- printArray
- echo -n "Enter the element to be searched:"
- read s
- binarySearch
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement