• Welcome to Bashguru

    Linux is one of popular version of UNIX operating System. It is open source as its source code is freely available. It is free to use. Linux was designed considering UNIX compatibility. It's functionality list is quite similar to that of UNIX and become very popular over the last several years. Our Basic motive is to provide latest information about Linux Operating system.

  • Python Programming

    Python is a comparatively simple programming language, compared to c++. Although some of the benefits of c++ are abstracted away in python, they are replaced with an overall easier to learn language with many “intuitive” features. For this reason it is common and recommended by most professionals that people new to programming start with python.

  • Perl Programming

    Perl is an open-source, general-purpose interpreted programming language. Used often for CGI, Perl is also used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications. The Perl languages borrow features from other programming languages including C, shell scripting (sh), AWK, and sed. They provide powerful text processing facilities without the arbitrary data-length limits of many contemporary UNIX command line tools, facilitating easy manipulation of text files.

  • Android

    Android is an operating system based on the Linux kernel, and designed primarily for touch screen mobile devices such as smart phones and tablet computers. Android is a Linux-based software system, and similar to Linux, is free and open source software. This means that other companies can use the Android operating developed by Google and use it in their mobile devices.Android gives you a world-class platform for creating apps and games for Android users everywhere, as well as an open marketplace for distributing to them instantly.

Monday, January 3, 2011

Posted by venu k
11 comments | 3:28 AM

#!/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

11 comments:

  1. Perfect - just what I was looking for. Thank you!

    ReplyDelete
  2. thank you very much.
    You saved me for 2marrow test.

    keep posting.

    ReplyDelete
  3. if suppose i want to return the array what should i do???

    ReplyDelete
  4. Thanks for the program!!!!

    ReplyDelete
  5. can you help me with the shell script of finding number of occurrences of an element in an array?

    ReplyDelete
  6. Thanks! This article was very helpful and saved me many hours of digging and experimentation.

    ReplyDelete
  7. Thank you sir, this is a Fabulous article. Thanks for sharing with us. I learn many things from this article.
    Let's try to play run 3 game now.

    ReplyDelete
  8. 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

    ReplyDelete
  9. 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