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<n;i++)) do read arr[$i] done #logic for selection sort for((i=0;i<n-1;i++)) do small=${arr[$i]} index=$i for((j=i+1;j<n;j++)) do if((arr[j]<small)) then small=${arr[$j]} index=$j fi done temp=${arr[$i]} arr[$i]=${arr[$index]} arr[$index]=$temp done #printing sorted array echo "printing sorted array" for((i=0;i<n;i++)) do echo ${arr[$i]} done
Output
Enjoy :)
If You Liked This Post Please Take a Time To Share This Post
0 comments:
Post a Comment