© All Rights Reserved

12 views

© All Rights Reserved

- Perl-SWIG
- Excel Formulas
- Binary Search
- VHDL Report
- An Improved Meta-heuristic Search for Constrained Interaction Testing 2009
- Yii for Beginners
- Introduction.to.Algorithms
- C-Programming_syllabus.pdf
- Advanced Data Structures
- Algorithms
- 12 Computer Science CBSE Exam Papers 2014 Delhi Set 1
- Manual 14
- Homework 5 - Report
- Search&Sort c Programs
- Archos 70d EReader PN110170 Book
- CS 211 Lecture 03 - Types of Data Structures
- 04162518
- Mat Lab 270 Notes
- I year lab manual R 2004
- Operation and Data Mapping For

You are on page 1of 3

Given a sorted array of N elements, it is tempting to say that in average each element would

takes (1+logN)/2 to be found successfully. However, this formula does not take into account the

fact that each element in the array requires different number of iterations in the binary search

before it is found.

Take the following array of 15 elements (Figure 1) as an example:

Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Values 5 15 25 35 45 55 65 75 85 95 105 115 125 135 145

Figure 1. A sorted array of 15 integers

As shown in the binarySearch( ) method definition (see

http://users.cis.fiu.edu/~weiss/dsj4/code/BinarySearch.java), in each iteration the value of mid is

updated ( mid = ( low + high ) / 2 ) . The element at position 7, for example, always takes a

single iteration to be found. On the other hand, data at position 14 takes 4 iterations in the binary

search before being found. Figure 2 shows a binary tree that illustrates the four cases of

successful binary search, each of which takes a different number of iterations. For example,

elements at positions 1, 5, 9, and 13 each takes three iterations before being found, while

elements at positions 3 and 11 each takes only two iterations to be found.

mid

low high

Iteration 1 0,7,14

Figure 2. A binary tree showing the number of iterations for each element in an array of 15

to be found via binary search

Table 1 summarizes the array elements and the respective number of iterations for them to be

found. The rightmost column shows the percentage of elements for each case. For instance,

about 50% (8 out of 15) of the nodes take 4 iterations in the binary search before being found.

Number of iterations Array elements Percentage of nodes

1 A[7] ~6.25%

2 A[3], A[11] ~12.5%

3 A[1], A[5], A[9], A[13] ~25%

4 A[0], A[2], A[4], A[6], A[8], A[10], A[12], ~ 50%

A[14]

Table 1. Array elements are divided into four cases, each with different number of

iterations.

To simplify the calculation, let N be equal to 2k 1 (i.e., k ~= logN). The correct formula to

calculate the average number of iterations for successful find is shown below.

=1 ( ) ( ) / N

= =1 2

/N

= (1*2 + + (logN-1)* 22 + logN* 2 ) / N <Sequence 1>

Take the array of 15 elements as an example, the average cost is shown below:

4=1( ) ( ) / 15

~= 3.26 (or ~logN)

3. The conclusion

The average cost of a successful search is about the same as the worst case where an item is not

found in the array, both being roughly equal to logN.

So, the average and the worst case cost of binary search, in big-O notation, is O(logN).

Exercises:

1. Take an array of 31 elements. Generate a binary tree and a summary table similar to those

in Figure 2 and Table 1.

2. Calculate the average cost of successful binary search in a sorted array of 31 elements.

3. Given an array of N elements, prove that calculation of Sequence 1 shown above is

indeed O(logN).

Programming projects:

- Perl-SWIGUploaded byGaurav Pant
- Excel FormulasUploaded byhradmin99
- Binary SearchUploaded byThomas Ramos
- VHDL ReportUploaded byMohamed Zanaty
- An Improved Meta-heuristic Search for Constrained Interaction Testing 2009Uploaded byRajvardhan Singh
- Yii for BeginnersUploaded byhermanchester
- Introduction.to.AlgorithmsUploaded bydiego
- C-Programming_syllabus.pdfUploaded byJai Sharma
- Advanced Data StructuresUploaded byShareef Khan
- AlgorithmsUploaded byMohammad Gulam Ahamad
- 12 Computer Science CBSE Exam Papers 2014 Delhi Set 1Uploaded byAbhishek Dargan
- Manual 14Uploaded byray72ro
- Search&Sort c ProgramsUploaded bysanjeetha
- Homework 5 - ReportUploaded bySerhat Can
- Archos 70d EReader PN110170 BookUploaded byAlvaro Cortes de la Mira
- CS 211 Lecture 03 - Types of Data StructuresUploaded byChi Chay Martinez
- 04162518Uploaded bySudhakar Spartan
- Mat Lab 270 NotesUploaded byChaubey Ajay
- I year lab manual R 2004Uploaded byPushpavalli Kathirvelu
- Operation and Data Mapping ForUploaded byJoel Ortiz Sosa
- Module a Exercise > ModA2_QB08_engUploaded byapi-3812894
- TCC102-Unit1- Part_1_-_Array_Revised_ApplicationUploaded byapi-26781128
- exerciocios_programaacoUploaded byIgor Fonseca
- Cs8151 Unit II Notes1Uploaded byxa
- arrays1Uploaded byirlmaks
- Collections 2Uploaded byphani
- ppschedulingUploaded byAnkit Goyal
- C_chap06.pptUploaded byYusep Riyadi
- Basics of javaUploaded bythirupathi reddy
- Exam2 PracticeUploaded byWebdev88

- Advt Ceptam 08 AdvtUploaded byeldhose
- Christine Lagarde Speaks to NDTV on State of Indian EconomyUploaded bystarscrawl
- Davos 2018_ _Red Tape Out, Red Carpet in,_ PM Modi Tells Global CEOs - 10 FactsUploaded bystarscrawl
- AtmUploaded byarevazhagunvc
- LIC AAO 2016 Practice Set 1Uploaded bystarscrawl
- Blood Pressure Chart for AdultsUploaded bypsshnkr
- GRE Issue EssaysUploaded byPavithra Shivan
- Week 1 Assignment 1Uploaded bystarscrawl
- Current Affair 2012 13Uploaded bystarscrawl
- cvUploaded byAbhishek Mishra
- binsearchanswe.pdfUploaded bystarscrawl
- Binary SearchUploaded bystarscrawl
- Binary SearchUploaded bystarscrawl
- ChristmasTreeGiftBox-PrintableUploaded bystarscrawl
- A Recipe for Hydroponic Success.pdfUploaded byfshirani7619
- Important_Notice_01_07_2016.pdfUploaded bystarscrawl
- DRDO CEPTAM STA-B Computer Science SyllabusUploaded bystarscrawl
- New Syllabus Combined Civil Services II UpdatedUploaded bySuganth Infin
- rbi grade b Handout ExamUploaded bySneha Seth
- SSC CGL 2016 Official AdvertisementUploaded byAnonymous xdetBXSaot
- Important_Notice_01_07_2016.pdfUploaded bystarscrawl
- UPSC-Paper-2016-with-answers.pdfUploaded bystarscrawl
- Construction methodolgyUploaded bystarscrawl
- Evaluation of KeralaUploaded byAr Yamini Jain
- NBC 2005Uploaded bynvikki
- Guide to Academic and Scholarly WritingUploaded bystarscrawl
- scitech7Uploaded bystarscrawl
- fess1psUploaded byCecil Thompson

- Sgs Ssc Apqp and Ppap en 07Uploaded byvaishrahul07
- Steps to Debug Routines in BIUploaded bymig007
- Sony Hcd Gpx7Uploaded byBila Nicola Florin Sorin
- STUDIES ON URBAN SPRAWL AND SPATIAL PLANNING SUPPORT SYSTEM FOR BANGALORE, INDIAUploaded byPUSTAKA Virtual Tata Ruang dan Pertanahan (Pusvir TRP)
- 8 Sortation Module 2 Sorters AftersortsUploaded byautomotionconveyors
- Lowenthal mUploaded bySTr Dinu
- Circuit SymbolsUploaded byJuan Francisco Garcia Arroyo
- EdeUploaded bykxalxo7637
- Controller LoopUploaded byfrancisblesson
- jhstutorial1213-2739356Uploaded byShesh
- basic KundaliUploaded byDevendra Godambe
- Lab 1-LinuxUploaded byanubhav
- LA Ferrari Emergency Repsond GuideUploaded byCruzHeng09181
- Industry Index EnglishUploaded byfjvera1
- AcopleUploaded bycesar
- BS EN ISO 13337-2009Uploaded byalinor_tn
- SUCRALOSE2Uploaded byKristine Dwi Puspitasari
- Java Technical Test Set 1Uploaded bySoniya Ahuja
- Intellect Image GuideUploaded byTridentGumYum
- Hfss ExamplesUploaded bymykeenzo5658
- EES Program GuideUploaded byiosoma
- Developing a Family Nursing Care PlanUploaded byRN_kn
- Ni-Co Laterites - A Deposit Model by USGSUploaded byAhmad Ashari
- 734635_r4cfd12a94e18bUploaded bymedic_brinza
- The Freshwater Snail Genus Sulcospira TroschelUploaded byHusnisastra
- Bitumen ExtractionUploaded byAbdhul Khadhir Shalayar
- TS00400G Tech. HandbookUploaded byninisa01
- Rock Star Service CatalogueUploaded bybob panic
- Press Release CaneInfoUploaded byputhira
- Press Release - Autodesk Clean Tech Partner Program in SingaporeUploaded byHenry Wong