Quiz: Searching in a managed array


Sorry for my recent lack of blogging.  There's been a lot going on these days!


As the banner on my blog now indicates, you'll be able to catch me at two upcoming conferences.  As with TechEd in June, Sonja Keserovic and I will be giving an Interop presentation at this year's PDC in October.  This presentation will be brand new, though.  Feel free to make suggestions if you're dying to hear about certain topics!  One month before that, I'll be giving two presentations at WinSummit in Switzerland - one on PInvoke, and one on COM Interoperability.  Hopefully I'll see you there!


Yesterday I learned about something that surprised me.  Maybe it won't surprise you, but I'm going to share it anyway.  If it prevents just one person from introducing a subtle bug in their code, then it was worth it!


If you're writing managed code and you need to efficiently find elements in an array, you'll be happy to know that you don't have to write your own binary search algorithm.  System.Array has a static BinarySearch method!  (System.Collections.ArrayList has an instance BinarySearch method, too.)


So what's wrong with the following C# code?  Why does it print "Not found!"?


    // WARNING: This code has a bug!
    int [] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
    int index = Array.BinarySearch(numbers, 3);


    if (index < 0)
      Console.WriteLine("Not found!");
    else
      Console.WriteLine("Found at index " + index);

Comments (15)

  1. Ivaylo Bakalov says:

    The documentation says:
    "If the elements of array are not already sorted in increasing value according to the IComparable implementation, the result might be incorrect."

  2. torstenR says:

    Yes. The entries have to be sorted in increasing order to get correct results.

  3. Ken says:

    But with an appropriate comparer, it could be made to work.

    int index = Array.BinarySearch(numbers, 3, new DecreasingOrderIntComparer());

  4. Chris Martin says:

    Try this before calling BinarySearch()

    System.Array.Sort(numbers);

  5. Ok I see that there is already the answer I wanted to give 🙂 Fast Guys….

  6. Read Print says:

    Free books for students, teachers, and the classic enthusiast.

  7. Cydergoth says:

    One interesting thing you didn’t mention is that if you use the list version of this and the target is not found, that the search routine returns the ones complement of the appropriate point to insert it… like so:

    ArrayList values=new ArrayList();

    … put some values in here …

    int insertPoint=values.BinarySearch(target);

    if(insertPoint<0) values.Insert(~insertPoint,target);

    Note the tilde (~) in front of the insertPoint in the Insert method.

    This trick allows you to maintain a sorted list easily 😉

  8. Jeff Teacher says:

    breasts</a> – <a href="http://bondage-dominations-pics-stories.com/breast_cancer.html">breast cancer</a> – <a href="http://bondage-dominations-pics-stories.com/capital_punishment.html">capital punishment</a> – <a href="http://bondage-dominations-pics-stories.com/ticket_master.html">ticket master</a> – <a href="http://bondage-dominations-pics-stories.com/breast.html">breast</a&gt; – <a href="http://bondage-dominations-pics-stories.com/spanking.html">spanking</a&gt; – <a href="http://bondage-dominations-pics-stories.com/slavery.html">slavery</a&gt; – <a href="http://bondage-dominations-pics-stories.com/breast_implants.html">breast implants</a> – <a href="http://bondage-dominations-pics-stories.com/bondage.html">bondage</a&gt; – <a href="http://bondage-dominations-pics-stories.com/dungeons_and_dragons.html">dungeons and dragons</a> – <a href="http://bondage-dominations-pics-stories.com/leather.html">leather</a&gt; – <a href="http://bondage-dominations-pics-stories.com/leather_jackets.html">leather jackets</a> – <a href="http://bondage-dominations-pics-stories.com/bdsm.html">bdsm</a&gt; – <a href="http://bondage-dominations-pics-stories.com/rubber_stamps.html">rubber stamps</a> – <a href="http://bondage-dominations-pics-stories.com/shaved.html">shaved</a&gt; – <a href="http://bondage-dominations-pics-stories.com/back_pain.html">back pain</a> – <a href="http://bondage-dominations-pics-stories.com/torture.html">torture</a&gt; – <a href="http://bondage-dominations-pics-stories.com/leather_furniture.html">leather furniture</a> – <a href="http://bondage-dominations-pics-stories.com/latex.html">latex</a&gt; – <a href="http://bondage-dominations-pics-stories.com/leather_coats.html">leather coats</a> – <a href="http://bondage-dominations-pics-stories.com/breast_reduction.html">breast reduction</a> – <a href="http://bondage-dominations-pics-stories.com/breast_enlargement.html">breast enlargement</a> – <a href="http://bondage-dominations-pics-stories.com/femdom.html">femdom</a&gt; – <a href="http://bondage-dominations-pics-stories.com/dungeon_siege.html">dungeon siege</a> – <a href="http://bondage-dominations-pics-stories.com/corporal_punishment.html">corporal punishment</a> – <a href="http://bondage-dominations-pics-stories.com/mistress.html">mistress</a&gt; – <a href="http://bondage-dominations-pics-stories.com/extreme_hu.html">extreme hu</a> – <a href="http://bondage-dominations-pics-stories.com/milky_breasts.html">milky breasts</a> – <a href="http://bondage-dominations-pics-stories.com/breast_feeding.html">breast feeding</a> – <a href="http://bondage-dominations-pics-stories.com/tire_chains.html">tire chains</a> – <a href="http://bondage-dominations-pics-stories.com/fetish.html">fetish</a&gt; – <a href="http://bondage-dominations-pics-stories.com/breast_enhancement.html">breast enhancement</a> – <a href="http://bondage-dominations-pics-stories.com/chain_saws.html">chain saws</a> – <a href="http://bondage-dominations-pics-stories.com/gag_gifts.html">gag gifts</a> – <a href="http://bondage-dominations-pics-stories.com/spanking_stories.html">spanking stories</a> – <a href="http://bondage-dominations-pics-stories.com/search_engine_submission.html">search engine submission</a> – <a href="http://bondage-dominations-pics-stories.com/bizarre_pics.html">bizarre pics</a> – <a href="http://bondage-dominations-pics-stories.com/slaves.html">slaves</a&gt; – <a href="http://bondage-dominations-pics-stories.com/lower_back_pain.html">lower back pain</a> – <a href="http://bondage-dominations-pics-stories.com/bare_breasts.html">bare breasts</a> – <a href="http://bondage-dominations-pics-stories.com/slavery_pictures.html">slavery pictures</a> – <a href="http://bondage-dominations-pics-stories.com/shaved_beavers.html">shaved beavers</a> – <a href="http://bondage-dominations-pics-stories.com/alice_in_chains.html">alice in chains</a> – <a href="http://bondage-dominations-pics-stories.com/women_breasts.html">women breasts</a> – <a href="http://bondage-dominations-pics-stories.com/sluts.html">sluts</a&gt; – <a href="http://bondage-dominations-pics-stories.com/bizarre.html">bizarre</a&gt; – <a href="http://bondage-dominations-pics-stories.com/spankings.html">spankings</a&gt; – <a href="http://bondage-dominations-pics-stories.com/chest_pain.html">chest pain</a> – <a href="http://bondage-dominations-pics-stories.com/knee_pain.html">knee pain</a> – <a href="http://bondage-dominations-pics-stories.com/food_chain.html">food chain</a> – <a href="http://bondage-dominations-pics-stories.com/natural_breasts.html">natural breasts</a> – <a href="http://bondage-dominations-pics-stories.com/rubber_stamping.html">rubber stamping</a> – <a href="http://bondage-dominations-pics-stories.com/neck_pain.html">neck pain</a> – <a href="http://bondage-dominations-pics-stories.com/foot_pain.html">foot pain</a> – <a href="http://bondage-dominations-pics-stories.com/discipline.html">discipline</a&gt; – <a href="http://bondage-dominations-pics-stories.com/dominatrix.html">dominatrix</a&gt; – <a href="http://bondage-dominations-pics-stories.com/extreme_sports.html">extreme sports</a> – <a href="http://bondage-dominations-pics-stories.com/pain.html">pain</a&gt; – <a href="http://bondage-dominations-pics-stories.com/galleries_of_natural_breasts.html">galleries of natural breasts</a> – <a href="http://bondage-dominations-pics-stories.com/rotator_cuff.html">rotator cuff</a> – <a href="http://bondage-dominations-pics-stories.com/free_search_engine_submission.html">free search engine submission</a> – <a href="http://bondage-dominations-pics-stories.com/lactating_breasts.html">lactating breasts</a> – <a href="http://bondage-dominations-pics-stories.com/leather_pants.html">leather pants</a> – <a href="http://bondage-dominations-pics-stories.com/abdominal_pain.html">abdominal pain</a> – <a href="http://bondage-dominations-pics-stories.com/master.html">master</a&gt; – <a href="http://bondage-dominations-pics-stories.com/master_of_orion.html">master of orion</a> – <a href="http://bondage-dominations-pics-stories.com/extreme.html">extreme</a&gt; – <a href="http://bondage-dominations-pics-stories.com/wilson_leather.html">wilson leather</a> – <a href="http://bondage-dominations-pics-stories.com/handcuffs.html">handcuffs</a&gt; – <a href="http://bondage-dominations-pics-stories.com/leather_sofa.html">leather sofa</a> – <a href="http://bondage-dominations-pics-stories.com/information_on_breast_cancer.html">information on breast cancer</a> – <a href="http://bondage-dominations-pics-stories.com/baring_their_breasts.html">baring their breasts</a> – <a href="http://bondage-dominations-pics-stories.com/big_breast_lovers.html">big breast lovers</a> – <a href="http://bondage-dominations-pics-stories.com/wilsons_leather.html">wilsons leather</a> – <a href="http://bondage-dominations-pics-stories.com/rope.html">rope</a&gt; – <a href="http://bondage-dominations-pics-stories.com/rubber.html">rubber</a&gt; – <a href="http://bondage-dominations-pics-stories.com/breast_implants_pics.html">breast implants pics</a> – <a href="http://bondage-dominations-pics-stories.com/american_slavery.html">american slavery</a> – <a href="http://bondage-dominations-pics-stories.com/shoulder_pain.html">shoulder pain</a> – <a href="http://bondage-dominations-pics-stories.com/breast_pumps.html">breast pumps</a> – <a href="http://bondage-dominations-pics-stories.com/sub_seven.html">sub seven</a> – <a href="http://bondage-dominations-pics-stories.com/shaved_pussy.html">shaved pussy</a> – <a href="http://bondage-dominations-pics-stories.com/food_chains.html">food chains</a> – <a href="http://bondage-dominations-pics-stories.com/halle_berry_breast.html">halle berry breast</a> – <a href="http://bondage-dominations-pics-stories.com/punishment_enemas.html">punishment enemas</a> – <a href="http://bondage-dominations-pics-stories.com/forced_sex.html">forced sex</a> – <a href="http://bondage-dominations-pics-stories.com/slut.html">slut</a&gt; – <a href="http://bondage-dominations-pics-stories.com/breast_augmentation.html">breast augmentation</a> – <a href="http://bondage-dominations-pics-stories.com/chain_mail.html">chain mail</a> – <a href="http://bondage-dominations-pics-stories.com/tandy_leather.html">tandy leather</a> – <a href="http://bondage-dominations-pics-stories.com/hip_pain.html">hip pain</a> – <a href="http://bondage-dominations-pics-stories.com/foot_fetish.html">foot fetish</a> – <a href="http://bondage-dominations-pics-stories.com/leg_pain.html">leg pain</a> – <a href="http://bondage-dominations-pics-stories.com/chain_link_fence.html">chain link fence</a> – <a href="http://bondage-dominations-pics-stories.com/heel_pain.html">heel pain</a> – <a href="http://bondage-dominations-pics-stories.com/leather_lingerie.html">leather lingerie</a> – <a href="http://bondage-dominations-pics-stories.com/pain_management.html">pain management</a> – <a href="http://bondage-dominations-pics-stories.com/multiple_breasts.html">multiple breasts</a> – <a href="http://bondage-dominations-pics-stories.com/breasts_pics.html">breasts pics</a> – <a href="http://bondage-dominations-pics-stories.com/supply_chain_management.html">supply chain management</a> – <a href="http://bondage-dominations-pics-stories.com/photos_of_lactating_breasts.html">photos of lactating breasts</a> – <a href="http://bondage-dominations-pics-stories.com/stomach_pain.html">stomach pain</a> – <a href="http://bondage-dominations-pics-stories.com/slave.html">slave</a&gt; – <a href="http://bondage-dominations-pics-stories.com/snow_chains.html">snow chains</a> – <a href="http://bondage-dominations-pics-stories.com/chain_saw.html">chain saw</a> – <a href="http://bondage-dominations-pics-stories.com/spank.html">spank</a&gt; – <a href="http://bondage-dominations-pics-stories.com/cock_and_ball_torture.html">cock and ball torture</a> – <a href="http://bondage-dominations-pics-stories.com/slave_trade.html">slave trade</a> – <a href="http://bondage-dominations-pics-stories.com/femdom_stories.html">femdom stories</a> – <a href="http://bondage-dominations-pics-stories.com/facial_humiliation.html">facial humiliation</a> – <a href="http://bondage-dominations-pics-stories.com/smoking_fetish.html">smoking fetish</a> – <a href="http://bondage-dominations-pics-stories.com/whipping.html">whipping</a&gt; – <a href="http://bondage-dominations-pics-stories.com/big_breasts.html">big breasts</a> – <a href="http://bondage-dominations-pics-stories.com/tit_torture.html">tit torture</a> – <a href="http://bondage-dominations-pics-stories.com/female_domination.html">female domination</a> – <a href="http://bondage-dominations-pics-stories.com/small_breasts.html">small breasts</a> – <a href="http://bondage-dominations-pics-stories.com/bondage_stories.html">bondage stories</a> – <a href="http://bondage-dominations-pics-stories.com/audio_slave.html">audio slave</a> – <a href="http://bondage-dominations-pics-stories.com/nipple_torture.html">nipple torture</a> – <a href="http://bondage-dominations-pics-stories.com/domestic_discipline.html">domestic discipline</a> – <a href="http://bondage-dominations-pics-stories.com/leather_sofas.html">leather sofas</a> – <a href="http://bondage-dominations-pics-stories.com/punishment.html">punishment</a&gt; – <a href="http://bondage-dominations-pics-stories.com/breast_pain.html">breast pain</a> – <a href="http://bondage-dominations-pics-stories.com/breast_cancer_photos.html">breast cancer photos</a> – <a href="http://bondage-dominations-pics-stories.com/capitol_punishment.html">capitol punishment</a> – <a href="http://bondage-dominations-pics-stories.com/bondages_pics.html">bondages pics</a> – <a href="http://bondage-dominations-pics-stories.com/puffy_breasts.html">puffy breasts</a> – <a href="http://bondage-dominations-pics-stories.com/spanking_children.html">spanking children</a> – <a href="http://bondage-dominations-pics-stories.com/rate_my_breasts.html">rate my breasts</a> – <a href="http://bondage-dominations-pics-stories.com/pain_killers.html">pain killers</a> – <a href="http://bondage-dominations-pics-stories.com/coach_leather.html">coach leather</a> – <a href="http://bondage-dominations-pics-stories.com/spanking_videos.html">spanking videos</a> – <a href="http://bondage-dominations-pics-stories.com/kidney_pain.html">kidney pain</a> – <a href="http://bondage-dominations-pics-stories.com/joint_pain.html">joint pain</a> – <a href="http://bondage-dominations-pics-stories.com/masters_of_the_universe.html">masters of the universe</a> – <a href="http://bondage-dominations-pics-stories.com/the_ropes.html">the ropes</a> – <a href="http://bondage-dominations-pics-stories.com/latex_clothing.html">latex clothing</a> – <a href="http://bondage-dominations-pics-stories.com/chronic_pain.html">chronic pain</a> – <a href="http://bondage-dominations-pics-stories.com/self_bondage.html">self bondage</a> – <a href="http://bondage-dominations-pics-stories.com/jam_master_jay.html">jam master jay</a> – <a href="http://bondage-dominations-pics-stories.com/rubber_boots.html">rubber boots</a> – <a href="http://bondage-dominations-pics-stories.com/submissive.html">submissive</a&gt; – <a href="http://bondage-dominations-pics-stories.com/chain.html">chain</a&gt; – <a href="http://bondage-dominations-pics-stories.com/master_card.html">master card</a> – <a href="http://bondage-dominations-pics-stories.com/extreme_makeover.html">extreme makeover</a> – <a href="http://bondage-dominations-pics-stories.com/rope_lights.html">rope lights</a> – <a href="http://bondage-dominations-pics-stories.com/sub_zero.html">sub zero</a> – <a href="http://bondage-dominations-pics-stories.com/leather_chaps.html">leather chaps</a> – <a href="http://bondage-dominations-pics-stories.com/breast_reduction_surgery.html">breast reduction surgery</a> – <a href="http://bondage-dominations-pics-stories.com/key_chains.html">key chains</a> –

  9. Debt Loan says:

    very informative site with lots of useful info

  10. buy xanax says:

    i like your website very much but please do get us more information about it

  11. Syndy says:

    <a href="http://basto.coz.in/">xanax</a>”>http://basto.coz.in/">xanax</a> | [URL=http://basto.coz.in/]xanax[/URL]

Skip to main content