Lab
Code :- <! DOCTYPE html > < html > < head > < title > Binary Search Program </ title > < style > form { margin : 20px ; } label { display : block ; margin-bottom : 5px ; } input [ type = "number" ] { padding : 5px ; margin-bottom : 10px ; font-size : 16px ; } input [ type = "submit" ] { padding : 10px ; font-size : 16px ; background-color : #008CBA ; color : #FFF ; border : none ; border-radius : 5px ; cursor : pointer ; } </ style > </ head > < body > < form action = "binary_search.php" method = "post" > < label for = "size" > Enter array size: </ label > < input type = "number" id = "size" name = "size" required > < label for = "search" > Enter search element: </ label > < input type = "number" id = "search" name = "search" required > < label for = "elements" > Enter array elements: </ label > < input type = "number" id = "elements" name = "elements" placeholder = "Enter element 1" required > < input type = "number" id = "elements" name = "elements" placeholder = "Enter element 2" required > < input type = "number" id = "elements" name = "elements" placeholder = "Enter element 3" required >
< input type = "number" id = "elements" name = "elements" placeholder = "Enter element 4" required > < input type = "number" id = "elements" name = "elements" placeholder = "Enter element 5" required > < input type = "submit" value = "Search" > </ form > </ body > </ html > Php code :- <?php function binarySearch ( Array $arr , $x ) { if ( count ( $arr ) === 0 ) return false ; $low = 0 ; $high = count ( $arr ) - 1 ; while ( $low <= $high ) { $mid = floor (( $low + $high ) / 2 ); if ( $arr [ $mid ] == $x ) { return true ; } if ( $x < $arr [ $mid ]) { $high = $mid - 1 ; } else { $low = $mid + 1 ; } } return false ; } // Driver code $arr = array ( 45 , 56 , 25 , 22 , 70 ); $value = 25 ; if ( binarySearch ( $arr , $value ) == true ) { echo $value . " Exists" ; }
else { echo $value . " Doesnt Exist" ; } ?> Screen shorts : - The $_SERVER['REQUEST_METHOD'] == 'POST' condition checks if the form has been submitted using the POST method.The $_POST superglobal variable retrieves the values of the form inputs.The for loop creates an array of elements based on the input values. The sort function sorts the array in ascending order.The while loop performs the binary search by dividing the array in half and checking if the middle element is equalto the search value. If it is, it outputs the index where the element is found and terminates the loop. Otherwise, it adjusts the left and right indices to narrow down the search range.
If the search value is not found in the array, the if statement outside the loop outputs a message indicating that the element was not found.Note that this code assumes that the form has input fields with the names "size", "search", and "elements[]" (with brackets to indicate that it's an array), which correspond to the array size, the search value, and the array elements respectively. Also, the array size input determines howmany array element input fields are displayed on the form.
Binary Search Program with PHP Implementation and Explanation
Please or to post comments