Linear Search
Write A Shell Script for Linear Search Shell Script: clear echo enter number of elements read n k=0 echo enter array elements for((i=1;i<=n;i++)) do read arr[$i] done echo the array for((i=1;i<=n;i++)) do echo ${arr[$i]} done echo enter the element you want to search read a for((i=1;i<=n;i++)) do if [ ${arr[$i]} -eq $a ] then k=1 break fi done if [ $k -eq 1 ] then echo element found at position $i else echo element not found fi OUTPUT: 1)[admin@localhost ~]$ sh lsearch.sh enter number of elements 4 enter array elements 67 90 23 45 the array 67 90 23 45 enter the element you want to search 23 element found at position 3 1)[admin@localhost ~]$ sh lsearch.sh enter number of elements 4 enter array elements 23 78 56 44 the array 23 78 56 44 enter the element you want to search 98 element not found
Tags:
Shell Script
0 comments