Q. How do I write selection sort in Bash
Ans:
#!/bin/bash
echo "enter the number"
read n
echo "enter number in an array"
for((i=0;i
read arr[$i]
done
#logic for selection sort
for((i=0;i
small=${arr[$i]}
index=$i
for((j=i+1;j
if((arr[j]then
small=${arr[$j]}
index=$j
fi
done
temp=${arr[$i]}
arr[$i]=${arr[$index]}
arr[$index]=$temp
done
#printing sorted
0 comments:
Post a Comment