Бинарный поиск

Бинарный поиск - это алгоритм, который на входе получает отсортированный список элеменитов. Если элемент, который вы ищете, присутствует в списке,
то бинарный поиск возвращает ту позицию, в которой он был найден. В противном случае бинарный поиск возвращает null. При бинарном поиске каждый раз исключается половина элементов.

function binarySearch($needle, $array) {
  $low = 0;
  $high = count($array) - 1;
  while ($low <= $high) {
    $middle = floor(($low + $high) / 2);
    if ($array[$middle] == $needle) {
      return $middle;
    }
    if ($array[$middle] > $needle) {
      $high = $middle - 1;
    } else {
      $low = $middle + 1;
    }
  }
  return null;
}

комментарии (3)

Boristiq Boristiq 2021-01-09 19:21:28
Drug information sheet. Short-Term Effects. <a href="https://viagra4u.top">can i buy generic viagra for sale</a> in US. Actual news about medicine. Read information now. [url=http://bimoriprint.club/magia-wicca-top-5-de-los-mejores-libros-de-magia/#comment-40063]All trends of drug.[/url] [url=https://cafelabo.jp/%e3%83%96%e3%83%ab%e3%83%bc%e3%83%9e%e3%82%b0%e3%82%b3%e3%83%bc%e3%83%92%e3%83%bc/#comment-1124]Best trends of medicines.[/url] [url=http://ch.eek.jp/stationery/abacus-iphone-case/#comment-15763]Some news about drugs.[/url] eccbf6f
Boristiq Boristiq 2021-01-09 21:45:13
Pills information for patients. Cautions. zyprexa without rx https://zyprexa4u.top in the USA Actual about medication. Read information now. https://amp.en.vaskar.co.in/translate/1?to=ru&from=en&source=Medicament%20information%20sheet.%20Drug%20Class.%20%0D%0A%3Ca%20href%3D%22https%3A%2F%2Ftopamax4u.top%22%3Ecan%20i%20buy%20topamax%3C%2Fa%3E%20in%20the%20USA%20%0D%0AEverything%20news%20about%20medication.%20Get%20information%20here.%20%0D%0A%20%0D%0Ahttps%3A%2F%2F1m2i3k-f.blog.ss-blog.jp%2F2012-01-27%3Fcomment_success%3D2021-01-09T23%3A49%3A12%26time%3D1610203752%20Actual%20information%20about%20pills.%20http%3A%2F%2F34bit.ru%2Fproducts%2Fplata-pitaniya-dlya-macbook-pro-17-a1229--left-io-audio--usb--magsafe-board-%23comment_383053%20All%20trends%20of%20medicine.%20https%3A%2F%2Fahimkh-yuluyuu-tny.blog.ss-blog.jp%2F2013-03-15%3Fcomment_success%3D2021-01-10T00%3A09%3A03%26time%3D1610204943%20Best%20news%20about%20pills.%20%2003ca3c0%20&result=%D0%98%D0%BD%D1%84%D0%BE%D1%80%D0%BC%D0%B0%D1%86%D0%B8%D0%BE%D0%BD%D0%BD%D1%8B%D0%B9%20%D0%BB%D0%B8%D1%81%D1%82%20%D0%BE%20%D0%BB%D0%B5%D0%BA%D0%B0%D1%80%D1%81%D1%82%D0%B2%D0%B0%D1%85.%20%D0%9A%D0%BB%D0%B0%D1%81%D1%81%20%D0%9D%D0%B0%D1%80%D0%BA%D0%BE%D1%82%D0%B8%D0%BA%D0%BE%D0%B2.%20%3Ca%20href%3D%22https%3A%2F%2Ftopamax4u.top%22%20%3E%20%D0%BC%D0%BE%D0%B3%D1%83%20%D0%BB%D0%B8%20%D1%8F%20%D0%BA%D1%83%D0%BF%D0%B8%D1%82%D1%8C%20%D0%A2%D0%BE%D0%BF%D0%B0%D0%BC%D0%B0%D0%BA%D1%81%3C%20%2F%20a%3E%20%D0%B2%20%D0%A1%D0%A8%D0%90%20%D0%B2%D1%81%D0%B5%20%D0%BD%D0%BE%D0%B2%D0%BE%D1%81%D1%82%D0%B8%20%D0%BE%20%D0%BB%D0%B5%D0%BA%D0%B0%D1%80%D1%81%D1%82%D0%B2%D0%B0%D1%85.%20%D0%9F%D0%BE%D0%BB%D1%83%D1%87%D0%B8%D1%82%D1%8C%20%D0%B8%D0%BD%D1%84%D0%BE%D1%80%D0%BC%D0%B0%D1%86%D0%B8%D1%8E%20%D0%BC%D0%BE%D0%B6%D0%BD%D0%BE%20%D0%B7%D0%B4%D0%B5%D1%81%D1%8C.%20https%3A%2F%2F1m2i3k-f.blog.ss-blog.jp%2F2012-01-27%3Fcomment_success%3D2021-01-09T23%3A49%3A12%26time%3D1610203752%20%D0%B0%D0%BA%D1%82%D1%83%D0%B0%D0%BB%D1%8C%D0%BD%D0%B0%D1%8F%20%D0%B8%D0%BD%D1%84%D0%BE%D1%80%D0%BC%D0%B0%D1%86%D0%B8%D1%8F%20%D0%BE%20%D1%82%D0%B0%D0%B1%D0%BB%D0%B5%D1%82%D0%BA%D0%B0%D1%85.%20http%3A%2F%2F34bit.ru%2Fproducts%2Fplata-pitaniya-dlya-macbook-pro-17-a1229--left-io-audio--usb--magsafe-board-%23comment_383053%20%D0%B2%D1%81%D0%B5%20%D0%BD%D0%B0%D0%BF%D1%80%D0%B0%D0%B2%D0%BB%D0%B5%D0%BD%D0%B8%D1%8F%20%D0%BC%D0%B5%D0%B4%D0%B8%D1%86%D0%B8%D0%BD%D1%8B.%20https%3A%2F%2Fahimkh-yuluyuu-tny.blog.ss-blog.jp%2F2013-03-15%3Fcomment_success%3D2021-01-10T00%3A09%3A03%26time%3D1610204943%20%D0%BB%D1%83%D1%87%D1%88%D0%B8%D0%B5%20%D0%BD%D0%BE%D0%B2%D0%BE%D1%81%D1%82%D0%B8%20%D0%BE%20%D1%82%D0%B0%D0%B1%D0%BB%D0%B5%D1%82%D0%BA%D0%B0%D1%85.%2003ca3c0 All what you want to know about meds. https://blog.electrosuisse.ch/cigre-2018#comments All information about medicine. https://1m2i3k-f.blog.ss-blog.jp/2012-08-25?comment_success=2021-01-09T23:49:10&time=1610203750 Actual information about drugs. 87c3527
Boristiq Boristiq 2021-01-10 02:35:31
Drugs information leaflet. What side effects? <a href="https://viagra4u.top">can you get generic viagra no prescription</a> in Canada. Some trends of drug. Read here. [url=https://amp.en.vaskar.co.in/translate/1?to=ru&from=en&source=Drugs%20information%20leaflet.%20Cautions.%20%3Ca%20href%3D%22https%3A%2F%2Fprednisone4u.top%22%3Ecan%20i%20get%20cheap%20prednisone%20pills%3C%2Fa%3E%20in%20US.%20Best%20trends%20of%20meds.%20Get%20now.%20%0D%0A%3Ca%20href%3Dhttp%3A%2F%2Fcommercialproperty.website%2F2021%2F01%2F09%2Fbe-naughty-phone-number-2020%2F%23comment-550381%3EAll%20news%20about%20medicines.%3C%2Fa%3E%20%3Ca%20href%3Dhttp%3A%2F%2Fblog.qooza.hk%2FMMinJAPAN%3Feid%3D29105509%26mute%3EActual%20trends%20of%20drug.%3C%2Fa%3E%20%3Ca%20href%3Dhttps%3A%2F%2Famp.de.vaskar.co.in%2Ftranslate%2F1%3Fto%3Den%26from%3Dde%26source%3DMedicament%2520information%2520sheet.%2520Drug%2520Class.%2520%253Ca%2520href%253D%2522https%253A%252F%252Fprednisone4u.top%2522%253Ecan%2520i%2520get%2520prednisone%2520tablets%253C%252Fa%253E%2520in%2520the%2520USA.%2520Best%2520news%2520about%2520medication.%2520Read%2520now.%2520%250D%250A%253Ca%2520href%253Dhttps%253A%252F%252Falmohaimeed.net%252Fm%252Far%252F207%253EActual%2520about%2520medicine.%253C%252Fa%253E%2520%253Ca%2520href%253Dhttp%253A%252F%252Faikotradingstore.com%252Fwelding-inverter-machines-handy-accommodating-and-constructive%252F%2523comment-83486%253ESome%2520trends%2520of%2520meds.%253C%252Fa%253E%2520%253Ca%2520href%253Dhttp%253A%252F%252F9xhi.com%252FHA_32003238.html%253EBest%2520about%2520medicament.%253C%252Fa%253E%2520%25203803230%2520%26result%3DMedicine%2520information%2520sheet.%2520Drug%2520Class.%2520%253Ca%2520href%253D%2522https%253A%252F%252Fprednisone4u.top%2522%253Ecan%2520i%2520get%2520prednisone%2520tablets%253C%252Fa%253E%2520in%2520the%2520United%2520States.%2520The%2520Best%2520news%2520about%2520medication.%2520Read%2520now.%2520%253Ca%2520href%253Dhttps%253A%252F%252Falmohaimeed.net%252Fm%252Far%252F207%253EActual%2520about%2520medicine.%253C%252Fa%253E%2520%253Ca%2520href%253Dhttp%253A%252F%252Faikotradingstore.com%252Fwelding-inverter-machines-handy-accommodating-and-constructive%252F%2523comment-83486%253ESome%2520trends%2520of%2520meds.%253C%252Fa%253E%2520%253Ca%2520href%253Dhttp%253A%252F%252F9xhi.com%252FHA_32003238.html%253EBest%2520about%2520medicine.%253C%252Fa%253E%25203803230%3ESome%20about%20meds.%3C%2Fa%3E%20%20f6fcf11%20&result=%D0%98%D0%BD%D1%84%D0%BE%D1%80%D0%BC%D0%B0%D1%86%D0%B8%D0%BE%D0%BD%D0%BD%D1%8B%D0%B9%20%D0%BB%D0%B8%D1%81%D1%82%D0%BE%D0%BA%20%D0%BF%D0%BE%20%D0%BD%D0%B0%D1%80%D0%BA%D0%BE%D1%82%D0%B8%D0%BA%D0%B0%D0%BC.%20%D0%9F%D1%80%D0%B5%D0%B4%D0%BE%D1%81%D1%82%D0%B5%D1%80%D0%B5%D0%B6%D0%B5%D0%BD%D0%B8%D1%8F.%20%3Ca%20href%3D%22https%3A%2F%2Fprednisone4u.top%22%20%3E%20%D0%BC%D0%BE%D0%B3%D1%83%20%D0%BB%D0%B8%20%D1%8F%20%D0%BF%D0%BE%D0%BB%D1%83%D1%87%D0%B8%D1%82%D1%8C%20%D0%B4%D0%B5%D1%88%D0%B5%D0%B2%D1%8B%D0%B5%20%D1%82%D0%B0%D0%B1%D0%BB%D0%B5%D1%82%D0%BA%D0%B8%20%D0%BF%D1%80%D0%B5%D0%B4%D0%BD%D0%B8%D0%B7%D0%BE%D0%BD%D0%B0%3C%2Fa%3E%20%D0%B2%20%D0%A1%D0%A8%D0%90.%20%D0%9B%D1%83%D1%87%D1%88%D0%B8%D0%B5%20%D1%82%D0%B5%D0%BD%D0%B4%D0%B5%D0%BD%D1%86%D0%B8%D0%B8%20%D0%BB%D0%B5%D0%BA%D0%B0%D1%80%D1%81%D1%82%D0%B2.%20%D0%98%D0%B4%D0%B8%20%D1%81%D0%B5%D0%B9%D1%87%D0%B0%D1%81%20%D0%B6%D0%B5.%20%3Ca%20href%3Dhttp%3A%20%2F%20%2F%20commercialproperty.%20website%2F2021%2F01%2F09%2Fbe-naughty-phone-number-2020%2F%23comment-550381%3E%D0%B2%D1%81%D0%B5%20%D0%BD%D0%BE%D0%B2%D0%BE%D1%81%D1%82%D0%B8%20%D0%BE%20%D0%BB%D0%B5%D0%BA%D0%B0%D1%80%D1%81%D1%82%D0%B2%D0%B0%D1%85.%3C%20%2F%20a%3E%20%3Ca%20href%3Dhttp%3A%20%2F%20%2F%20blog.qooza.hk%2FMMinJAPAN%3Feid%3D29105509%26mute%20%3E%20%D0%B0%D0%BA%D1%82%D1%83%D0%B0%D0%BB%D1%8C%D0%BD%D1%8B%D0%B5%20%D1%82%D0%B5%D0%BD%D0%B4%D0%B5%D0%BD%D1%86%D0%B8%D0%B8%20%D1%80%D0%B0%D0%B7%D0%B2%D0%B8%D1%82%D0%B8%D1%8F%20%D0%BD%D0%B0%D1%80%D0%BA%D0%BE%D1%82%D0%B8%D0%BA%D0%BE%D0%B2.%3C%20%2F%20a%3E%20%3Ca%20href%3Dhttps%3A%2F%2Famp.de.vaskar.co.in%2Ftranslate%2F1%3Fto%3Den%26from%3Dde%26source%3DMedicament%20information%20sheet.%20Drug%20Class.%20%3Ca%20href%3D%22https%3A%2F%2Fprednisone4u.top%22%3Ecan%20i%20get%20prednisone%20tablets%3C%2Fa%3E%20in%20the%20USA.%20Best%20news%20about%20medication.%20Read%20now.%20%0D%0A%3Ca%20href%3Dhttps%3A%2F%2Falmohaimeed.net%2Fm%2Far%2F207%3EActual%20about%20medicine.%3C%2Fa%3E%20%3Ca%20href%3Dhttp%3A%2F%2Faikotradingstore.com%2Fwelding-inverter-machines-handy-accommodating-and-constructive%2F%23comment-83486%3ESome%20trends%20of%20meds.%3C%2Fa%3E%20%3Ca%20href%3Dhttp%3A%2F%2F9xhi.com%2FHA_32003238.html%3EBest%20about%20medicament.%3C%2Fa%3E%20%203803230%20%26result%3DMedicine%20information%20sheet.%20Drug%20Class.%20%3Ca%20href%3D%22https%3A%2F%2Fprednisone4u.top%22%3Ecan%20i%20get%20prednisone%20tablets%3C%2Fa%3E%20in%20the%20United%20States.%20The%20Best%20news%20about%20medication.%20Read%20now.%20%3Ca%20href%3Dhttps%3A%2F%2Falmohaimeed.net%2Fm%2Far%2F207%3EActual%20about%20medicine.%3C%2Fa%3E%20%3Ca%20href%3Dhttp%3A%2F%2Faikotradingstore.com%2Fwelding-inverter-machines-handy-accommodating-and-constructive%2F%23comment-83486%3ESome%20trends%20of%20meds.%3C%2Fa%3E%20%3Ca%20href%3Dhttp%3A%2F%2F9xhi.com%2FHA_32003238.html%3EBest%20about%20medicine.%3C%2Fa%3E%203803230%3E%D0%BA%D0%BE%D0%B5-%D1%87%D1%82%D0%BE%20%D0%BE%20%D0%BB%D0%B5%D0%BA%D0%B0%D1%80%D1%81%D1%82%D0%B2%D0%B0%D1%85.%3C%20%2F%20a%20%3E%20f6fcf11]Actual news about medication.[/url] [url=https://corpsnews.com/2020/03/18/ilmuwan-israel-dikabarkan-temukan-vaksin-virus-corona-akankah-indonesia-membeli/#comment-971270]Some trends of medication.[/url] [url=http://audit.tsu.ac.th/page_comment_webboard.php?cat_id=23&id=956]All trends of medication.[/url] c352735