11 August 2012

Selection Sort using Shell Script

Leave a Comment

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

selection sort in ubuntu

Enjoy :)


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

0 comments:

Post a Comment