Saturday, August 11, 2012
9:16 PM

Selection Sort using Shell Script


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;ido
read arr[$i]
done
#logic for selection sort
for((i=0;ido
small=${arr[$i]}
index=$i
for((j=i+1;jdo
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