多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# Chapter 8: More Computational Complexity鈥擳he Searching Problem ## Overview Recall from the beginning of [Chapter 1](LiB0008.html#16) that Barney Beagle could find Colleen Collie's phone number quickly using a modified binary search. Barney may now be wondering if he could develop an even faster method for locating Colleen's number. We analyze the Searching problem next to determine whether this is possible. Like sorting, searching is one of the most useful applications in computer science. The problem is usually to retrieve an entire record based on the value of some key field. For example, a record may consist of personal information, whereas the key field may be the social security number. Our purpose here is similar to that in the preceding chapter. We want to analyze the problem of searching and show that we have obtained searching algorithms whose time complexities are about as good as our lower bounds. Additionally, we want to discuss the data structures used by the algorithms and to discuss when a data structure satisfies the needs of a particular application. In [Section 8.1](LiB0068.html#763), we obtain lower bounds for searching for a key in an array only by comparisons of keys (as we did for sorting in the preceding chapter), and we show that the time complexity of Binary Search ([Algorithms 1.5](LiB0009.html#38) and [2.1](LiB0068.html#783)) is as good as the bounds. In searching for a phone number, Barney Beagle actually uses a modification of Binary Search called "Interpolation Search," which does more than just compare keys. That is, when looking for Colleen Collie's number, Barney does not start in the middle of the phone book because he know that the names beginning with C are near the front. He "interpolates" and starts near the front of the book. We present Interpolation Search in [Section 8.2](LiB0069.html#789). In [Section 8.3](LiB0070.html#794), we show that an array does not meet other needs (besides the searching) of certain applications. Therefore, although Binary Search is optimal, the algorithm cannot be used for some applications because it relies on an array implementation. We show that trees do meet these needs, and we discuss tree searching. [Section 8.4](LiB0071.html#807) concerns searching when it is not important that the data ever be retrieved in sorted sequence. We discuss hashing in [Section 8.4](LiB0071.html#807). [Section 8.5](LiB0072.html#819) concerns a different searching problem, the Selection problem. This problem is to find the *k*th-smallest (or *k*th-largest) key in a list of *n* keys. In [Section 8.5](LiB0072.html#819) we introduce adversary arguments, which are another means of obtaining bounds for the performance of all algorithms that solve a problem.