0% found this document useful (0 votes)
43 views

' Write A Script To Demonstrate A Linear Search

The document discusses linear and binary search algorithms for searching arrays. It provides: 1) A description of linear and binary search techniques for locating a value in an array. Linear search compares each element to the target until a match is found, while binary search eliminates half of remaining elements after each comparison. 2) An example JavaScript program that demonstrates linear search on an array of 100 even numbers. It displays whether the target value is found. 3) An explanation that linear search is efficient for small arrays but binary search should be used for large sorted arrays, as it eliminates half of remaining elements at each step. 4) An outline for a binary search demonstration program to search a sorted array and display the

Uploaded by

Dinesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

' Write A Script To Demonstrate A Linear Search

The document discusses linear and binary search algorithms for searching arrays. It provides: 1) A description of linear and binary search techniques for locating a value in an array. Linear search compares each element to the target until a match is found, while binary search eliminates half of remaining elements after each comparison. 2) An example JavaScript program that demonstrates linear search on an array of 100 even numbers. It displays whether the target value is found. 3) An explanation that linear search is efficient for small arrays but binary search should be used for large sorted arrays, as it eliminates half of remaining elements at each step. 4) An outline for a binary search demonstration program to search a sorted array and display the

Uploaded by

Dinesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

2.

16 Searching Arrays: Linear Search and Binary Search


At times, large amount of data might be stored in arrays. The programmer might need to search
whether a particular value is present in the array or not. That particular value used to find whether it is
present in an array or not is referred to as “key value”. The process of locating a particular element
value in an array is called “searching”.
There are two searching techniques.
(1) Linear search
(2) Binary search
5. ` Write a script to demonstrate a linear search.
<HTML>
<HEAD>
<TITL E>Linear Search of an Array</TITLE>
<SCRIPT LANGUAGE =”JavaScript”>
Var a = new Array (100);
For (var i=0; i<a.length; ++i)
A [i]=2*I;
Function button pressed ()
{
Var searchkey = search form. Inputval . Value;
Var element = linear Search (a, parselnt (SearchKey));
If (element !=-1)
Search form. Result. Value =
“Found value in element “ + element;
Else
Search Form. result. Value = “value not found”,.
}
Function linear Search (the Array, key)
{
For (var n = 0; n<the Array. length; ++n)
If (the Array [n]==key)
return n ;
return -1
}
2.16 Searching Arrays: Linear Search and Binary Search
At times, large amount of data might be stored in arrays. The programmer might need to search
whether a particular value is present in the array or not. That particular value used to find whether it is
present in an array or not is referred to as “key value”. The process of locating a particular element
value in an array is called “searching”.
There are two searching techniques.
(1) Linear search
(2) Binary search
5. ` Write a script to demonstrate a linear search.
<HTML>
<HEAD>
<TITL E>Linear Search of an Array</TITLE>
<SCRIPT LANGUAGE =”JavaScript”>
Var a = new Array (100);
For (var i=0; i<a.length; ++i)
A [i]=2*I;
Function button pressed ()
{
Var searchkey = search form. Inputval . Value;
Var element = linear Search (a, parselnt (SearchKey));
If (element !=-1)
Search form. Result. Value =
“Found value in element “ + element;
Else
Search Form. result. Value = “value not found”,.
}
Function linear Search (the Array, key)
{
For (var n = 0; n<the Array. length; ++n)
If (the Array [n]==key)
return n ;
return -1
}
</SCRIPT
</HEAD>
<BODY>
<FORM NAME = “search Form”>
<p>Enter integer search key<BR>
<INPUT NAME=”input val”TYPE =”text”>
<INPUT NAME = “search” TYPE = “button “VALUE = “Search”
ONCLICK = “button Pressed () “>< BR> </IP>
<P> Result <BR>
<INPUT NAME = “result” TYPE = “text “SIZE = “30”></p>
< /FORM>
</BODY>
</HTML>
Out put

Explanation

In the script the function “linear Search “ uses a for loop containing an if structure to compare
each element of an array with a search key. If the search key is found, the function returns the subscript
value for the element to indicate the exact position of the search key in the array. If the array being
searched is not in any particular order, it is likely that the value will be found in the first or last element.
Or last element. Therefore, on an average, the program will have to compare the search key with half
the elements of the array.

The program contains a 100 elements array filled with even integers from 0to 198. The user types the
search key in the text field and presses the “search” button. The result will be displayed in the next text
field.
The linear search function works well for small arrays or for unsorted arrays. But for large arrays, linear
search is inefficient. If the array is sorted, the high speed binary search technique can be used.
After each comparison, the binary search eliminates half of the elements in the array being
searched. The binary search algorithm locates the middle array element and compares it to the search
key. If they are equal, the search key has been found and the subscript of that element is returned.
Otherwise the problem is reduced to searching half of the array. If the search key is less than the middle
array element, the first half of the array is searched, other wise the second half of the array is searched.
6. Write a script to demonstrate the binary search.
<HTML>
<HEAD>
<TITLE>Binary Search of an Array <TITLE>
<SCRIPT LANGUAGE = “JavaScript”>
Var a =new Array(15);
For (var i=0;<a.length;++i)
a[i]=2*i;
Function button pressed ()
{
Var searchkey = search form. Inputval. Value;
Search form. Result. Value = “Portions of array searched\n”;
Var element = binary Search (a, Parselnt (searchkey));
If (element!=-1)
Search form. Result. Value +=
“\n Found value in element “+element;
else
Search form. result. Value +=”\n value not found”;
}
Function binary Search (the Array, key)
{
Var low =0;
Var high =the Array. Length -1;
Var middle;
While (low <=high){
Middle = (low +high)/2;
build Out put (the Array, low1, middle, high);
If (key = = the Array [middle])
return middle;
else if (key < the Array [middle])
high = middle -1;
else
low = middle – 1;
}
return -1;
}
Function builds out put (the Array, low, mid, high)
{
For (var i=0; i<the Array.length; i++) {
If (i<low \\i>high)
Search Form. result. Value +=” “;
else if (i= =mid) // mark middle element in output
Search Form. result. Value + = a [I] +
(the Array [I] < 10? “ * “ :”*”);
else
Search For m. result. Value + = a [I ] +
(the Array [ I ]<10? “ “ : “ “);
}
Search Form. Result. Value + = “\n”;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME =”SEARCH Form “>
<p>Enter integer search key <BR>
<INPUT NAME = “input Val” TYPE = “text”>
<INPUTNAME = “search “TYPE = “button” VALUE =”Search”
ONCLICK = “button Pressed ()”><BR></P>
<P>Result <BR><TEXTAREA NAME = “result “ROWS = “7” COLS =”60”>
</TEXTAREA></P>
</FORM>
</BODY>
</HTML>

You might also like