Saturday, August 11, 2012
12:32 AM

Insertion Sort Using Shell Script


Q. How do I write Insertion 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 insertion sort
for((i=1;ido
j=$i-1
temp=${arr[$i]}
while((j>=0 && arr[j]>temp))
do
arr[$j+1]=${arr[$j]}
j=$j-1
done
arr[j+1]=$temp
done
#printing sorted array
echo "printing sorted array"
for((i=0;ido
echo $

0 comments:

Post a Comment