exercism

Exercism solutions.
git clone git://code.dwrz.net/exercism
Log | Files | Refs

README.md (2832B)


      1 # Binary Search
      2 
      3 Implement a binary search algorithm.
      4 
      5 Searching a sorted collection is a common task. A dictionary is a sorted
      6 list of word definitions. Given a word, one can find its definition. A
      7 telephone book is a sorted list of people's names, addresses, and
      8 telephone numbers. Knowing someone's name allows one to quickly find
      9 their telephone number and address.
     10 
     11 If the list to be searched contains more than a few items (a dozen, say)
     12 a binary search will require far fewer comparisons than a linear search,
     13 but it imposes the requirement that the list be sorted.
     14 
     15 In computer science, a binary search or half-interval search algorithm
     16 finds the position of a specified input value (the search "key") within
     17 an array sorted by key value.
     18 
     19 In each step, the algorithm compares the search key value with the key
     20 value of the middle element of the array.
     21 
     22 If the keys match, then a matching element has been found and its index,
     23 or position, is returned.
     24 
     25 Otherwise, if the search key is less than the middle element's key, then
     26 the algorithm repeats its action on the sub-array to the left of the
     27 middle element or, if the search key is greater, on the sub-array to the
     28 right.
     29 
     30 If the remaining array to be searched is empty, then the key cannot be
     31 found in the array and a special "not found" indication is returned.
     32 
     33 A binary search halves the number of items to check with each iteration,
     34 so locating an item (or determining its absence) takes logarithmic time.
     35 A binary search is a dichotomic divide and conquer search algorithm.
     36 
     37 ## Getting Started
     38 
     39 Make sure you have read the "Guides" section of the
     40 [C track](https://exercism.io/my/tracks/c) on the Exercism site. This covers
     41 the basic information on setting up the development environment expected
     42 by the exercises.
     43 
     44 
     45 ## Passing the Tests
     46 
     47 Get the first test compiling, linking and passing by following the [three
     48 rules of test-driven development][3-tdd-rules].
     49 
     50 The included makefile can be used to create and run the tests using the `test`
     51 task.
     52 
     53     make test
     54 
     55 Create just the functions you need to satisfy any compiler errors and get the
     56 test to fail. Then write just enough code to get the test to pass. Once you've
     57 done that, move onto the next test.
     58 
     59 [3-tdd-rules]: http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd
     60 
     61 As you progress through the tests, take the time to refactor your
     62 implementation for readability and expressiveness and then go on to the next
     63 test.
     64 
     65 Try to use standard C99 facilities in preference to writing your own
     66 low-level algorithms or facilities by hand.
     67 
     68 ## Source
     69 
     70 Wikipedia [http://en.wikipedia.org/wiki/Binary_search_algorithm](http://en.wikipedia.org/wiki/Binary_search_algorithm)
     71 
     72 ## Submitting Incomplete Solutions
     73 It's possible to submit an incomplete solution so you can see how others have completed the exercise.