Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A <a href="http://en.wikipedia.org/wiki/Linear_search" rel="noreferrer">linear search</a> looks down a list, one item at a time, without jumping. In complexity terms this is an <code>O(n)</code> search - the time taken to search the list gets bigger at the same rate as the list does.</p> <p>A <a href="http://en.wikipedia.org/wiki/Binary_search" rel="noreferrer">binary search</a> is when you start with the middle of a sorted list, and see whether that's greater than or less than the value you're looking for, which determines whether the value is in the first or second half of the list. Jump to the half way through the sublist, and compare again etc. This is pretty much how humans typically look up a word in a dictionary (although we use better heuristics, obviously - if you're looking for "cat" you don't start off at "M"). In complexity terms this is an <code>O(log n)</code> search - the number of search operations grows more slowly than the list does, because you're halving the "search space" with each operation.</p> <p>As an example, suppose you were looking for U in an A-Z list of letters (index 0-25; we're looking for the value at index 20).</p> <p>A linear search would ask:</p> <blockquote> <p><code>list[0] == 'U'</code>? No.<br/> <code>list[1] == 'U'</code>? No.<br/> <code>list[2] == 'U'</code>? No.<br/> <code>list[3] == 'U'</code>? No.<br/> <code>list[4] == 'U'</code>? No.<br/> <code>list[5] == 'U'</code>? No.<br/> ... <code>list[20] == 'U'</code>? Yes. Finished.</p> </blockquote> <p>The binary search would ask:</p> <blockquote> <p>Compare <code>list[12]</code> ('M') with 'U': Smaller, look further on. (Range=13-25)<br/> Compare <code>list[19]</code> ('T') with 'U': Smaller, look further on. (Range=20-25)<br/> Compare <code>list[22]</code> ('W') with 'U': Bigger, look earlier. (Range=20-21)<br/> Compare <code>list[20]</code> ('U') with 'U': Found it! Finished.<br/> </p> </blockquote> <p>Comparing the two:</p> <ul> <li>Binary search requires the input data to be sorted; linear search doesn't</li> <li>Binary search requires an <em>ordering</em> comparison; linear search only requires equality comparisons</li> <li>Binary search has complexity O(log n); linear search has complexity O(n) as discussed earlier</li> <li>Binary search requires random access to the data; linear search only requires sequential access (this can be very important - it means a linear search can <em>stream</em> data of arbitrary size)</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload