Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /bin/bash
- exchange(){
- temp=${a[$1]}
- a[$1]=${a[$2]}
- a[$2]=$temp
- }
- echo -n "Enter an array: "
- read -a a
- n=${#a[@]}
- echo $n
- echo -n "B-Bubble Sort I-Insertion Sort S-Selection Sort: "
- read ch
- case $ch in
- B):
- for(( i=0; i<$n; i++ )){
- for(( j=$i+1; j<$n; j++ )){
- if [ ${a[$i]} -gt ${a[$j]} ]; then
- exchange $i $j
- fi
- }
- };;
- I):
- for(( i=1; i<$n; i++ )){
- value=${a[$i]}
- j=$i
- while(( j>0 && ${a[$j-1]}>value ))
- do
- a[$j]=${a[$j-1]}
- j=$j-1
- done
- a[$j]=$value
- };;
- S):
- for(( i=0; i<$n-1; i++ )){
- min=$i
- for(( j=$i+1; j<$n; j++ )){
- if [ ${a[$j]} -lt ${a[$min]} ]; then
- min=$j;
- fi
- }
- if [ $min -ne $i ]; then
- exchange $min $i
- fi
- };;
- *): echo "Invalid option."
- esac
- echo ${a[*]}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement