' Write A Script To Demonstrate A Linear Search
' Write A Script To Demonstrate A Linear Search
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>