Binary Search is a searching technique used to search for an item in a sorted array. Here $data_array is a sorted array of items, and $search_item is the item we are looking at in the $data_array. Also, we need to pass the start and end index of the $data_array with the function call.
$data_array = [1,3,6,8,10,18,22,23,30,35,37,40,51,53,58,60,67,71,73,89,99];
$search_item = 8;
$start = 0;
$end = count($data_array) - 1;
function binary_search( $data_array, $search_item, $start, $end ){
while($start <= $end){
$mid = floor( ( $start + $end ) / 2 );
if( $search_item < $data_array[$mid] ){
$end = $mid - 1;
}elseif( $search_item > $data_array[$mid] ){
$start = $mid + 1;;
}else{
return $mid;
}
}
return -1;
}
$itm_indx = binary_search( $data_array, $search_item, $start, $end );
if( $itm_indx < 0 ){
echo 'item not found';
}else{
echo 'item found in index = '.$itm_indx;
}
Plugins to Enhance Your WooCommerce Store
Carefully selected tools to help you customize, manage, and optimize your WooCommerce experience.
Woo Parent Category Selector
- Automatically assign parent categories when child categories are selected
- Prevent accidental removal of required parent categories
- Automatically repairs missing parent category assignments during product save
- Bulk repair existing WooCommerce product category structures
- Keeps product category structures organized and consistent
Product Catalog Mode For WooCommerce
- Turn your WooCommerce store into catalog mode instantly
- Hide Add to Cart buttons and product prices conditionally
- Apply rules by user role, country, or scheduled time periods
- Display custom messages and inquiry options for products
- Control visibility settings globally or per product
Remove Add to Cart Button for WooCommerce
- Remove Add to Cart buttons from shop and product pages
- Hide buttons conditionally for selected products or categories
- Replace Add to Cart with custom text or messages
- Control button visibility by user role and product type
- Lightweight settings interface with easy customization
Remove Product Content for WooCommerce
- Hide product title, image, price, rating, and descriptions
- Remove tabs, meta information, SKU, tags, and related products
- Control visibility separately for shop and single product pages
- Customize WooCommerce layouts without coding
- Enable or disable content elements with simple settings
Really Simple XML and HTML Sitemap
- Generate both XML and HTML sitemaps automatically
- Display sitemaps anywhere using ShortCode support
- Include pages, posts, and custom post types
- Exclude selected pages or posts easily
- Supports WPML and Polylang multilingual plugins
