# 1 introduction to algorithms ## Binary search Binary search is an algorithm; its input is a **sorted list** of elements ![](https://hackmd.io/_uploads/SkMyLuyrn.jpg) ![](https://hackmd.io/_uploads/Skfg8ukSn.jpg) example ![](https://hackmd.io/_uploads/Bkv9ruyS3.png) ![](https://hackmd.io/_uploads/B1E8IOJr2.jpg) ![](https://hackmd.io/_uploads/HJ7vLdJr3.jpg) ## Algorithm running times grow at different rates ![](https://hackmd.io/_uploads/H119Ldkr3.jpg) ![](https://hackmd.io/_uploads/H1nqLOyrh.jpg) ![](https://hackmd.io/_uploads/S1m3Ld1H3.jpg) ![](https://hackmd.io/_uploads/rJApLdkH3.jpg) ## Big O establishes a worst-case run time Q: Suppose you’re using simple search to look for a person in the phone book. In this case, you’re looking for Adit. This guy is the first entry in your phone book. So you didn’t have to look at every entry—you found it on the first try. Did this algorithm take O(n) time? Or did it take O(1) time because you found the person on the first try? Q: `O(n + 26)`, `O(n - 26)`, `O(n * 26)`, `O(n / 26)`. Which is the fastest? ## The traveling salesperson ![](https://hackmd.io/_uploads/SJDqudJrh.jpg) ![](https://hackmd.io/_uploads/BJR5OOyr3.jpg) ![](https://hackmd.io/_uploads/BJthO_JH2.jpg) There are 120 permutations with 5 cities, so it will take 120 operations to solve the problem for 5 cities. it's `O(n!)` ![](https://hackmd.io/_uploads/rkHeYu1rn.jpg) ![](https://hackmd.io/_uploads/SkW-t_kS3.jpg) The best we can do is come up with an approximate solution; see chapter 10 for more.