Giter Club home page Giter Club logo

binary-search's Introduction

Binary search

Implement a binary search algorithm that takes in two parameters, list and searchValue, and returns a boolean value indicating whether the input searchValue exists in the input list. For example:

const nameList = [
    'Aaron',
    'Andy',
    'Batman',
    'Betsy',
    'Boba',
    'Bonnie',
    'Clarence',
    'Daisy',
    'Elektra',
    'Flash'
];

binarySearch(nameList, 'Daisy'); //=> true
binarySearch(nameList, 'Bruce'); //=> false

Notice that this function signature is exactly the same as any other search algorithm - it has a target value to search within a list, and if that value is found in that list, it returns either a boolean value indicating its existence, or the found value itself (eg. an object).

Binary search is one of the most commonly used and practical search algorithms out there because of its simplicity and efficiency.

The beauty in binary search is the fact that you've probably used in your life before, but you just may not have noticed it. The classic illustration is searching through a phone book.

How binary search works

Pretend you have a thick phone book. Each page has several entries of names, followed by the person's phone number next to it on the same line, and all the names are sorted by alphabetical order, so "Aaron" appears before "Andy". How would you go about searching for someone named "Steve"?

Naturally, you'll start by flipping the book to the middle and see what the first alphabet of the first name on the left page is. It turns out that it's a "G". And because you know that "S" happens later in the alphabetical system than "G", what you'll naturally do next is flip to the middle of the right-half of the book, and repeat the same evaluation again until you find a page with names starting with "S".

That, in essence, is the binary search algorithm. You half a list, discard one half and continue searching through the remaining half, until you arrive (or not) at the value you're searching for. Voila! You found what you wanted without iterating from the first item through to the last in the list - spending much less time for the same result.

Now try and implement this algorithm in code!

Note: The only drawback of binary search is that it only works if the array is sorted. Remember to always only feed a sorted array into the algorithm, or else it will very likely return an erroneous result!

Further - with objects

Create a new phonebook array that holds objects instead of strings. Each object should have 3 key-value pairs: name, phone, and id.

Modify your binary search algorithm to search for a given id (not name) value stored in the object, and return the object itself instead of a boolean value.

For example, binarySearch(phonebook, 44) should return { name: 'Daisy', phone: 12345678, id: 44 } (example) instead of true. It should still return false if no matches were found.

Hint: remember, binary search only works on sorted arrays. In this case, ensure your array is sorted by id value before passing it into your algorithm.

You can use the included phonebook.json data to save time and focus on writing your algorithm.

Further - search by any attribute

Modify your algorithm to take in 3 instead of 2 parameters, with the additional parameter being the name of any attribute to search the list by. For example:

binarySearch(phonebook, attribute, value);

// example
binarySearch(phonebook, 'phone', 12345678);

Hint: To search by any attribute, you'll probably need to first implement a sorting algorithm within your binarySearch algorithm, then conduct the binary search in the newly sorted array.

Resources

binary-search's People

Contributors

nickangtc avatar phangster avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.