#!/bin/bash
# SCRIPT : linearsearch.sh
# USAGE : linearsearch.sh
# PURPOSE: Searches given number in a list.
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
# A variation of Here Document permits "commenting out" data block.
: <<DATABLOCK
In computer science, linear search or sequential search is a method
for finding a particular value in a list, that consists in checking
every one of its elements, one at a time and in sequence, until the
desired one is found.
This is a very straightforward loop comparing every element in the
array with the key. As soon as an equal value is found, it returns.
If the loop finishes without finding a match, the search failed and
-1 is returned.
For small arrays, a linear search is a good solution because it's so
straightforward. In an array of a million elements, a linear search
will take,on average, 500,000 comparisons to find the key. For a much
faster search, take a look at binary search.
DATABLOCK
#####################################################################
# Define Functions Here #
#####################################################################
lsearch()
{
status=-1
for((i=0;i<count;i++))
do
Temp=$1
if [ $Temp -eq ${ARRAY[i]} ]
then
status=0
searches=$((i+1))
return
# return $((i+1))
# Bash function can return value between 0-255, That's why I assigned
# result to a global variable. This is one of the method to capture
# return value of a function.
fi
done
}
#####################################################################
# Variable Declaration #
#####################################################################
clear
echo "Enter Array Elements : "
read -a ARRAY
count=${#ARRAY[@]}
search=y
#####################################################################
# Main Script Starts Here #
#####################################################################
while [ "$search" == "y" -o "$search" == "Y" ]
do
echo -n "Enter element to be searched : "
read num
lsearch $num
if [ $status -eq 0 ]
then
echo "$num found after $searches searches"
else
echo "$num not found"
fi
echo -n "Do you want another search (y/n): "
read search
done
OUTPUT:
$ sh linearsearch.sh
Enter Array Elements :
12 34 56 78 90 23 45 56 67 321 66 88 92
Enter element to be searched : 56
56 found after 3 searches
Do you want another search (y/n): y
Enter element to be searched : 321
321 found after 10 searches
Do you want another search (y/n): y
Enter element to be searched : 100
100 not found
Do you want another search (y/n): n
Monday, January 3, 2011
Posted by venu k
11 comments | 3:28 AM
Subscribe to:
Post Comments (Atom)
Perfect - just what I was looking for. Thank you!
ReplyDeletethank you very much.
ReplyDeleteYou saved me for 2marrow test.
keep posting.
if suppose i want to return the array what should i do???
ReplyDeleteThanks for the program!!!!
ReplyDeletecan you help me with the shell script of finding number of occurrences of an element in an array?
ReplyDeleteThanks! This article was very helpful and saved me many hours of digging and experimentation.
ReplyDeleteC Math Examples
ReplyDeleteThank you sir, this is a Fabulous article. Thanks for sharing with us. I learn many things from this article.
ReplyDeleteLet's try to play run 3 game now.
Individuals who are having monetary issues and terrible credit in the meantime are denied out appropriately by these budgetary organizations. 24/7 car title loans chicago
ReplyDeleteNice blog and absolutely outstanding. You can do something much better but i still say this perfect.Keep trying for the best.
ReplyDeleteEmbedded System training in Chennai | Embedded system training institute in chennai | PLC Training institute in chennai | IEEE final year projects in chennai | VLSI training institute in chennai
Borrowers who are feeling urgent and need to acquire their money rapidly may not set aside the opportunity to ask what rate they will get.auto title loans chicago
ReplyDelete