Saturday, August 11, 2012

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

:) :)) ;(( :-) =)) ;( ;-( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ $-) (b) (f) x-) (k) (h) (c) cheer
Click to see the code!
To insert emoticon you must added at least one space before the code.