Binary Search WEEK 5 LESSON 1

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/6

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

7 Terms

1
New cards

Describe the steps involved in a binary search to find the value 47 in the list below.

4, 7, 8, 21, 46, 47, 51

Find the midpoint of the data which is element three value 21.

if (21) is equal to our search item (47) search item is found false in this case.

If search item is greater than the midpoint of 21 search second half of the list.

If midpoint 47 is equal to 47 search item is found true and search

2
New cards

Binary Search code

array myIntegers[5] = [12,16,19,22,25]

start = 0 (start index)

end = 4 (end index)

found = false (searchItem has been found yet)

input searchItem(‘Please enter search item‘)

midPoint = 0

while (found == false AND start <= end)

midPoint = (start + end) DIV 2  

if (myIntegers[midPoint] == searchItem) then

found = true

elseif (myIntegers[midPoint] > searchItem)

end = midPoint - 1

else 

start = midPoint +1

endif

endwhile

 

if (found == true) then

print(“Item found at “ + midPoint)

endif


3
New cards

Can a binary search use strings?

Yes because we can convert the characters to its representative ASCII value

4
New cards

How must the data set be arranged for the binary search to work?

It must be a sorted list

5
New cards

Why would we use a binary search rather than a linear search?

For larger sets of data

6
New cards

Is a binary search always faster than a linear search?

Not always if the searchItem is at the start the linear search will have fewer passes

7
New cards

What are the similarities and differences between linear and binary search?

Linear search and Binary search through a array or list for specific item

Binary Search is more efficient for larger datasets

Binary search has to be sorted list