Reynald, to demonstrate binary search, steps through creating custom classes and methods. He talks through the process along the way so the student understands the basic requirement for a binary search. Once done with the core functionality, he then writes out the code to execute the demonstration and runs it while explaining the results.
- [Instructor] Okay, so we are ready…to code our binary search algorithm.…To get started, let's go ahead and create a method…called binarySearch.…It's going to return an integer, and we're calling…it binarySearch, which accepts an array of integers,…and we'll call that a,…and the value that we're searching for,…that's our next parameter,…and that'll be x.…
To cease its complaining, I will return a value…of negative one, which is going to be what's going to happen…if we don't find what we're searching for,…and that'll be the last line.…Now, the first thing we're going to do…is to initialize the variable p.…But before I even do that,…why don't I go ahead and add in some comments…so we can keep track of the variables that we're using?…I'll just go ahead and paste that in.…
So as you remember, a is going to stand for the array.…X is what we are searching for.…P is going to represent the first index position;…q, the midpoint, and r, the last index position.…So let's do this in the steps that we reviewed.…Now, right off the bat, returning negative one…
- Working with linked lists
- Creating a stack
- Defining a queue
- Performing a binary search
- Finding target values with a linear search
Skill Level Beginner
1. Algorithms and Data Structure Overview
Reasons for this course1m 26s
2. Linked List
5. Binary Search
6. Linear Search
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.