12 views

Uploaded by starscrawl

- News
- Document for programming in JAVA
- L4 Arrays
- Week_7
- Php Tutorial
- MatDescargableRAP3 Ingles
- New Text Document
- Arrays n Collections
- At the End of This Lab Exercise
- algorithms made easy
- Data Structures_All Chapters - Wiki Books, Open Books for an Open World
- Exercise Instructions
- 12 Computer Science Mixed Test 04
- Midterm1 Solution
- Civil Details
- 2016+Journals+Price+List+-+USD
- 10_2 Pointers and Arrays_ Pointer Arithmetic
- ExcelDynamicArraysStraightToThePointV2e
- Excel Functions
- DYNMEMALL

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:

- NewsUploaded byhigorc
- Document for programming in JAVAUploaded byAngad Sood
- L4 ArraysUploaded byArjun Talwar
- Week_7Uploaded byRam Lakhan Meena
- Php TutorialUploaded byHemanta Nayak
- MatDescargableRAP3 InglesUploaded byJuan Karlos Perez
- New Text DocumentUploaded byAhmad
- Arrays n CollectionsUploaded byshwetajuneja
- At the End of This Lab ExerciseUploaded byOPie UPie
- algorithms made easyUploaded bySyed Ahsan
- Data Structures_All Chapters - Wiki Books, Open Books for an Open WorldUploaded byArnav Arora
- Exercise InstructionsUploaded byEric Zarate Zarlink
- 12 Computer Science Mixed Test 04Uploaded byMuraly Kirupa
- Midterm1 SolutionUploaded byMADDY
- Civil DetailsUploaded bytvargasn
- 2016+Journals+Price+List+-+USDUploaded byAndrey Yohanes Soaviou Simanjuntak
- 10_2 Pointers and Arrays_ Pointer ArithmeticUploaded bygdskumar
- ExcelDynamicArraysStraightToThePointV2eUploaded byRami Majdi
- Excel FunctionsUploaded bykbose29
- DYNMEMALLUploaded byRevathi Durai
- alv blockUploaded byAbid Sheik
- ECE246Uploaded byMikhail Kalashnikov
- Determination of the Punching Forces by LabView Software ApplicationsUploaded byphukaifr
- Us 8533139Uploaded byKroze
- One D Array ExerciseUploaded byAyesha Khan
- Loving LispUploaded bybodgergely
- Historical DataUploaded byAsriani Hasan
- Logix5000 Controllers Add-On Instructions Programming ManualUploaded byKhalid Baye
- Array RecordUploaded bySuman Kaur
- Iconos SapUploaded byRodrigo Vargas

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

- 1 Year Lab Python 2017 ManualUploaded byAnonymous pHX5EaA
- CS301 NotesUploaded byRizwanIlyas
- Data Structures for Range SearchingUploaded byMuhedin Hadzic
- 13-treesUploaded byLakshay Virmani
- Graph AnalyticsUploaded bymbala86
- Algorithm BookUploaded bybcdchetan
- C++ Ders8 - DizinlerUploaded byŞemsettin karakuş
- Afi Samples NewUploaded byantiw
- practical file questions2.docxUploaded byMayank Jha
- NR-10203- C and Data StructuresUploaded bySrinivasa Rao G
- BUploaded byAnonymous V7P5fNQI
- Topcoder- completeUploaded byArnav Kansal
- Lecture 11 h.avlUploaded byjnfz
- Cs1201 Design and Analysis of AlgorithmUploaded bymaharajaking
- data treeUploaded byhazel_ela
- OOP In JavaUploaded byOleksiy Kovyrin
- Multilevel IndexesUploaded byIndranil Sikder
- 140 Google Interview Questions for InterviewUploaded byRaunak Ramakrishnan
- Software Developer Interview QuestionsUploaded byAhmet Yerli
- CompterpracticalfileUploaded bySiddharth Banyal
- AlgorithmUploaded bysankar_mca227390
- ECE242.project1Uploaded byJoshua Yi
- ADA Module2 2Uploaded bygayathri
- Unot 3 Notes TreesUploaded bykingraaja
- Puzzels With SolutionsUploaded byChandrashekhar Yadav M
- Symbol TableUploaded byksai.mb
- Binary Search TreeUploaded byBilo Bilo
- BTech II Year Electronics Syllabus (Effective From 2017-18)Uploaded byWE THE GAMERS
- Bài tập phân tích thiết kế giải thuậtUploaded byHiệp Sĩ Giấy
- compiler_mod 5-symbol table.pptxUploaded byvidhya_bineesh