Binary search algorithm  Wikipedia, the free encyclopedia

 Valerie Beasley
 9 months ago
 Views:
Transcription
1 1 of 12 9/8/2010 6:37 PM Binary search algorithm From Wikipedia, the free encyclopedia In computer science, a binary search is an algorithm for locating the position of an item in a sorted array. [1][2] The idea is simple: compare the target to the middle item in the list. If the target is the same as the middle item, you've found the target. If it's before the middle item, repeat this procedure on the items before the middle. If it's after the middle item, repeat on the items after the middle. The method halves the number of items to check each time, so it finds it or determines it's not present in logarithmic time. A binary search is a dichotomic divide and conquer search algorithm. Binary search algorithm Class Search algorithm Data structure Array Worst case performance O(log n) Best case performance O(1) Average case performance O(log n) Worst case space complexity O(1) Contents 1 Overview 2 Examples 2.1 Number guessing game 2.2 Word lists 2.3 Applications to complexity theory 3 The method 3.1 That it works 3.2 That it is fast Average performance Inserting and deleting elements 3.3 Extensions 3.4 Variations Exclusive or inclusive bounds Deferred detection of equality Midpoint and width 4 Computer usage 4.1 The algorithm Numerical difficulties Syntax difficulties 5 See also 4.2 Implementations Iterative Threeway comparison Recursive Single comparison per iteration 4.3 Language support
2 2 of 12 9/8/2010 6:37 PM 6 References 7 External links Overview It is useful to find where an item is in a sorted array. For example, if you had an array for contact information, with people's names, addresses, and telephone numbers sorted by name, you could use binary search to find out a few useful facts: whether the person's information is in the array, what the person's address is, and what the person's telephone number is. Binary search will take far fewer comparisons than a linear search, but there are some downsides. Binary search can be slower than using a hash table. If items are changed, the array will have to be resorted so that binary search will work properly, which can take so much time that the savings from using binary search aren't worth it. If you can tell ahead of time that a few items are disproportionately likely to be sought, putting those items first and using a linear search could be much faster.. Examples Number guessing game This rather simple game begins something like "I'm thinking of an integer between forty and sixty inclusive, and to your guesses I'll respond 'High', 'Low', or 'Yes!' as might be the case." Supposing that N is the number of possible values (here, twentyone as "inclusive" was stated), then at most questions are required to determine the number, since each question halves the search space. Note that one less question (iteration) is required than for the general algorithm, since the number is already constrained to be within a particular range. Even if the number we're guessing can be arbitrarily large, in which case there is no upper bound N, we can still find the number in at most steps (where k is the (unknown) selected number) by first finding an upper bound by repeated doubling. [citation needed] For example, if the number were 11, we could use the following sequence of guesses to find it: 1, 2, 4, 8, 16, 12, 10, 11 One could also extend the technique to include negative numbers; for example the following guesses could be used to find 13: 0, 1, 2, 4, 8, 16, 12, 14, 13. Word lists People typically use a mixture of the binary search and interpolative search algorithms when searching a telephone book, after the initial guess we exploit the fact that the entries are sorted and can rapidly find the required entry. For example when searching for Smith, if Rogers and Thomas have been found, one can flip to a page about halfway between the previous guesses. If this shows Samson, we know that Smith is somewhere between the Samson and Thomas pages so we can bisect these. Applications to complexity theory Even if we do not know a fixed range the number k falls in, we can still determine its value by asking simple yes/no questions of the form "Is k greater than x?" for some number x. As a simple consequence of this, if you can answer the question "Is this integer property k greater than a given value?" in
3 3 of 12 9/8/2010 6:37 PM some amount of time then you can find the value of that property in the same amount of time with an added factor of log 2 k. This is called a reduction, and it is because of this kind of reduction that most complexity theorists concentrate on decision problems, algorithms that produce a simple yes/no answer. For example, suppose we could answer "Does this n x n matrix have determinant larger than k?" in O(n 2 ) time. Then, by using binary search, we could find the (ceiling of the) determinant itself in O(n 2 log d) time, where d is the determinant; notice that d is not the size of the input, but the size of the output. The method In order to discuss the method in detail, a more formal description is necessary. The basic idea is that there is a data structure represented by array A in which individual elements are identified as A(1), A(2),,A(N) and may be accessed in any order. The data structure contains a subelement or data field called here Key, and the array is ordered so that the successive values A(1).Key A(2).Key and so on. The requirement is that given some value x, find an index p (not necessarily the one and only) such that A(p).Key = x. To begin with, the span to be searched is the full supplied list of elements, as marked by variables L and R, and their values are changed with each iteration of the search process, as depicted by the flowchart. Note that the division by two is integer division, with any remainder lost, so that 3/2 comes out as 1, not 1½. The search finishes either because the value has been found, or else, the specified value is not in the list. That it works The method relies on and upholds the notion If x is to be found, it will be amongst elements (L + 1) to (R 1) of the array.
4 4 of 12 9/8/2010 6:37 PM The initialisation of L and R to 0 and N + 1 make this merely a restatement of the supplied problem, that elements 1 to N are to be searched, so the notion is established to begin with. The first step of each iteration is to check that there is something to search, which is to say whether there are any elements in the search span (L + 1) to (R 1). The number of such elements is (R L 1) so computing (R L) gives (number of elements + 1); halving that number (with integer division) means that if there was one element (or more) then p = 1 (or more), but if none p = 0, and in that case the method terminates with the report "Not found". Otherwise, for p > 0, the search continues with p:=l + p, which by construction is within the bounds (L + 1) to (R 1). That this position is at or adjacent to the middle of the span is not important here, merely that it is a valid choice. Now compare x to A(p).Key. If x = A(p).Key then the method terminates in success. Otherwise, suppose x < A(p).Key. If so, then because the array is in sorted order, x will also be less than all later elements of the array, all the way to element (R 1) as well. Accordingly, the value of the righthand bound index R can be changed to be the value p, since, by the test just made, x < A(p).Key and so, if x is to be found, it will be amongst elements earlier than p, that is (p 1) and earlier. And contrariwise, for the case x > A(p).Key, the value of L would be changed. Thus, whichever bound is changed the ruling notion is upheld, and further, the span remaining to be searched is reduced. If L is changed, it is changed to a higher number (at least L + 1), whereas if R is changed, it is to a lower number (at most R 1) because those are the limits for p. Should there have been just one value remaining in the search span (so that L + 1 = p = R 1), and x did not match, then depending on the sign of the comparison either L or R will receive the value of p and at the start of the next iteration the span will be found to be empty. Accordingly, with each iteration, if the search span is empty the result is "Not found", otherwise either x is found at the probe point p or the search span is reduced for the next iteration. Thus the method works, and so can be called an Algorithm. That it is fast With each iteration that fails to find a match at the probed position, the search is continued with one or other of the two subintervals, each at most half the size. More precisely, if the number of items, N, is odd then both subintervals will contain (N  1)/2 elements. If N is even then the two subintervals contain N/21 and N/2 elements. If the original number of items is N then after the first iteration there will be at most N/2 items remaining, then at most N/4 items, at most N/8 items, and so on. In the worst case, when the value is not in the list, the algorithm must continue iterating until the span has been made empty; this will have taken at most log 2 (N) + 1 iterations, where the notation denotes the floor function that rounds its argument down to an integer. This worst case analysis is tight: for any N there exists a query that takes exactly log 2 (N) + 1 iterations. When compared to linear search, whose worstcase behaviour is N iterations, we see that binary search is substantially faster as N grows large. For example, to search a list of 1 million items takes as much as 1 million iterations with linear search, but never more than 20 iterations with binary search. However, binary search is only valid if the list is in sorted order. Average performance There are two cases: for searches that will fail because the value is not in the list, the search span must be successively halved until no more elements remain and this process will require at most the p probes just defined, or one less. This latter occurs because the search span is not in fact exactly halved, and depending on the value of N and which elements of the list the absent value x is between, the span may be closed early.
5 5 of 12 9/8/2010 6:37 PM For searches that will succeed because the value is in the list, the search may finish early because a probed value happens to match. Loosely speaking, half the time the search will finish one iteration short of the maximum and a quarter of the time, two early. Consider then a test in which a list of N elements is searched once for each of the N values in the list, and determine the number of probes n for all N searches. N = n/n = 1 3/2 5/3 8/4 11/5 14/6 17/7 21/8 25/9 29/10 33/11 37/12 41/ In short log 2 (N) 1 is about the expected number of probes in an average successful search, and the worst case is log 2 (N), just one more probe. If the list is empty, no probes at all are made. Suppose the list to be searched contains N even numbers (say, 2,4,6,8 for N = 4) and a search is done for values 1, 2, 3, 4, 5, 6, 7, 8, and 9. The even numbers will be found, and the average number of iterations can be calculated as described. In the case of the odd numbers, they will not be found, and the collection of test values probes every possible position (with regard to the numbers that are in the list) that they might be not found in, and an average is calculated. The maximum value is for each N the greatest number of iterations that were required amongst the various trail searches of those N elements. The first plot shows the iteration counts for N = 1 to 63 (with N = 1, all results are 1), and the second plot is for N = 1 to The curve for the "found" searches approaches log 2 (N) 1 more closely for larger N as larger numbers of iterations are involved, in the same way that the successive summations 1/2, 1/4 + 1/2, 1/8 + 1/4 + 1/2 approach 1 as the number of terms increases: these are the probabilities of early detection of equality in successive iterations of a search. The slight bend in the curves within each iteration limit group is due to the early narrowing of the search bounds into two subspans whose lengths are often unequal. Iteration count for 1 N < 64
6 6 of 12 9/8/2010 6:37 PM Thus binary search is a logarithmic algorithm and executes in O(logN) time. In most cases it is considerably faster than a linear search. It can be implemented using iteration (as shown above), or recursion. In some languages it is more elegantly expressed recursively; however, in some Cbased languages tail recursion is not eliminated and the recursive version requires more stack space. Binary search can interact poorly with the memory hierarchy (i.e. caching), because of its randomaccess nature. For inmemory searching, if the span to be searched is small, a linear search may have superior performance simply because it exhibits better locality of reference. For external searching, care must be taken or each of the first several probes will lead to a disk seek. A common technique is to abandon binary Iteration count for 1 N < searching for linear searching as soon as the size of the remaining span falls below a small value such as 8 or 16 or even more in recent computers. The exact value depends entirely on the machine running the algorithm. Notice that for multiple searches with a fixed value for N, then (with the appropriate regard for integer division), the first iteration always selects the middle element at N/2, and the second always selects either N/4 or 3N/4, and so on. Thus if the array's key values are in some sort of slow storage (on a disc file, in virtual memory, not in the cpu's onchip memory), keeping those three keys in a local array for a special preliminary search will avoid accessing widely separated memory. Escalating to seven or fifteen such values will allow further levels at not much cost in storage. On the other hand, if the searches are frequent and not separated by much other activity, the computer's various storage control features will more or less automatically promote frequentlyaccessed elements into faster storage. When multiple binary searches are to be performed for the same key in related lists, fractional cascading can be used to speed up successive searches after the first one. Inserting and deleting elements The main disadvantage of sorted arrays is that inserting and deleting random elements executes in O(N) time. This is not true in all cases however. The search algorithm can be changed to check the last element first, which allows loading a sorted list at O(1) time per element. Extensions There is no particular requirement that the array being searched has the bounds 1 to N. It is possible to search a specified range, elements first to last instead of 1 to N. All that is necessary is that the initialisation be L:=first
7 7 of 12 9/8/2010 6:37 PM 1 and R:=last + 1, then all proceeds as before. The elements of the list are not necessarily all unique. If one searches for a value that occurs multiple times in the list, the index returned will be of the firstencountered equal element, and this will not necessarily be that of the first, last, or middle element of the run of equalkey elements but will depend on the positions of the values. Modifying the list even in seemingly unrelated ways such as adding elements elsewhere in the list may change the result. To find all equal elements an upward and downward linear search can be carried out from the initial result, stopping each search when the element is no longer equal. Thus, e.g. in a table of cities sorted by country, we can find all cities in a given country. Several algorithms closely related to or extending binary search exist. For instance, noisy binary search solves the same class of projects as regular binary search, with the added complexity that any given test can return a false value at random. (Usually, the number of such erroneous results are bounded in some way, either in the form of an average error rate, or in the total number of errors allowed per element in the search space.) Optimal algorithms for several classes of noisy binary search problems have been known since the late seventies, and more recently, optimal algorithms for noisy binary search in quantum computers (where several elements can be tested at the same time) have been discovered. Variations There are many, and they are easily confused. Also, binary search vs. selection sort is debatable. Exclusive or inclusive bounds The most significant differences are between the "exclusive" and "inclusive" forms of the bounds. This description uses the "exclusive" bound form, that is the span to be searched is (L + 1) to (R 1), and this may seem clumsy when the span to be searched could be described in the "inclusive" form, as L to R. Although the details differ the two forms are equivalent as can be seen by transforming one version into the other. The inclusive bound form may be attained by replacing all appearances of "L" by "(L 1)" and "R" by "(R + 1)" then rearranging. Thus, the initialisation of L:=0 becomes (L 1):=0 or L:=1, and R:=N + 1 becomes (R + 1):=N + 1 or R:=N. So far so good, but note now that the changes to L and R are no longer simply transferring the value of p to L or R as appropriate but now must be (R + 1):=p or R:=p 1, and (L 1):=p or L:=p + 1. Thus, the gain of a simpler initialisation, done once, is lost by a more complex calculation, and which is done for every iteration. If that is not enough, the test for an empty span is more complex also, as compared to the simplicity of checking that the value of p is zero. Nevertheless, the inclusive bound form is found in many publications, such as Donald Knuth. The Art of Computer Programming, Volume 3: Sorting and Searching, Third Edition. Deferred detection of equality Because of the syntax difficulties discussed below, so that distinguishing the three states <, =, and > would have to be done with two comparisons, it is possible to use just one comparison and at the end when the span is reduced to zero, equality can be tested for. The example distinguishes only < from >=. Midpoint and width An entirely different variation involves abandoning the L and R pointers in favour of a current position p and a width w where at each iteration, p is adjusted by + or w and w is halved. Professor Knuth remarks "It is
8 8 of 12 9/8/2010 6:37 PM possible to do this, but only if extreme care is paid to the details" Section 6.2.1, page 414 of The Art of Computer Programming, Volume 3: Sorting and Searching, Third Edition, outlines an algorithm, with the further remark "Simpler approaches are doomed to failure!" Computer usage The algorithm Although the basic idea of binary search is comparatively straightforward, the details can be surprisingly tricky Professor Donald Knuth When Jon Bentley assigned it as a problem in a course for professional programmers, he found that an astounding ninety percent failed to code a binary search correctly after several hours of working on it [3], and another study shows that accurate code for it is only found in five out of twenty textbooks (Kruse, 1999). Furthermore, Bentley's own implementation of binary search, published in his 1986 book Programming Pearls, contains an error that remained undetected for over twenty years. [4] Numerical difficulties In a practical implementation, the variables used to represent the indices will be of finite size, hence only capable of representing a finite range of values. For example, 16bit unsigned integers can only hold values from 0 to If the binary search algorithm is to operate on large arrays, this has two implications: The values first 1 and last + 1 must both be representable within the finite bounds of the chosen integer type. Therefore, continuing the 16bit example, the largest value that last may take is , not A problem exists even for the "inclusive" form of the method, as if x > A(65535).Key, then on the final iteration the algorithm will attempt to store into L and fail. Equivalent issues apply to the lower limit, where first 1 could become negative as when the first element of the array is at index zero. If the midpoint of the span is calculated as p := (L + R)/2, then the value (L + R) will exceed the number range if last is greater than (in our example) 65535/2 and the search wanders toward the upper end of the search space. This can be avoided by performing the calculation as p := (R  L)/2 + L. For example, this bug existed in Java SDK at Arrays.binarySearch() from 1.2 to 5.0 and fixed in 6.0 [5]. Syntax difficulties Another difficulty is presented by the absence in most computer languages of a threeway result from a comparison, which forces a comparison to be performed twice. The form is somewhat as follows: if a < b then action1 else if a > b then action2 else action3; About half the time, the first test will be true so that there will be only one comparison of a and b, but the other half of the time it will be false, and a second comparison forced. This is so grievous that some versions are recast so as not to make a second test at all thus not determining equality until the span has been reduced to zero, and thereby foregoing the possibility of early termination remember that about half the time the search will happen on a matching value one iteration short of the limit.
9 9 of 12 9/8/2010 6:37 PM It is quite easy to make this problem still worse (e.g. as in [6] ) by using an order such as if a = b then action3 else if a > b then action2 else action1; Rather than detecting equality early (as it might appear to), this will force two comparisons to be performed for all but the last iteration of a search. Implementations Iterative The following algorithm is slightly modified (to avoid overflow) from Niklaus Wirth's in Standard Pascal [7] : min := 1; max := N; {array size: var A : array [1..N] of integer} repeat mid := min + ((max  min) div 2); if x > A[mid] then min := mid + 1 else max := mid  1; until (A[mid] = x) or (min > max); This code uses inclusive bounds and a threeway test (for early loop termination in case of equality), but with two separate comparisons per iteration. It is not the most efficient solution. Threeway comparison Since Fortran does offer a threeway test, here is a version for searching an array of integers. For labels Fortran uses numbers at the start of statements, thus the 1, 2, 3, and 4. The if statement performs a go to to one of the three nominated labels according to the sign of its arithmetic expression. Integer Function BinarySearch(A,X,N)! Search for the value X in A(1)..A(N) Integer A(*),X!The array is indexed 1 to? Integer N!Stated number of elements. Integer L,R,P L = 0!Exclusive bounds, R = N + 1!To search elements 1 to N. 1 P = (R  L)/2!Probe; integer division. Not (L + R)/2! if (P <= 0) Return(L)!Search exhausted. P = L + P!Convert an offset from L to an array index. if (X  A(P)) 3,4,2!Test: negative,zero,positive. 2 L = P!A(P) < X. Shift the left bound up. go to 1 3 R = P!X < A(P). Shift the right bound down. go to 1 4 Return(P)!X = A(P). Found at index P. End Function BinarySearch Recursive
10 10 of 12 9/8/2010 6:37 PM The most straightforward implementation is recursive, which recursively searches the subrange dictated by the comparison: BinarySearch(A[0..N1], value, low, high) { if (high < low) return 1 // not found mid = low + ((high  low) / 2) if (A[mid] > value) return BinarySearch(A, value, low, mid1) else if (A[mid] < value) return BinarySearch(A, value, mid+1, high) else return mid // found } It is invoked with initial low and high values of 0 and N1. We can eliminate the tail recursion above and convert this to an iterative implementation: Single comparison per iteration In computer languages that lack a threeway comparison, the required threeway comparison has to be replaced by two twoway comparisons that would on average involve one and a half comparisons per iteration, not one. To reduce this overhead, some implementations defer checking for equality until after the search completes, as in this pseudocode: low = 0 high = N while (low < high) { mid = low + ((high  low) / 2) if (A[mid] < value) low = mid + 1; else //can't be high = mid1: here A[mid] >= value, //so high can't be < mid if A[mid] == value high = mid; } // high == low, using high or low depends on taste if ((low < N) && (A[low] == value)) return low // found else return 1 // not found This approach foregoes the possibility of early termination on discovery of a match, thus successful searches have log 2 (N) iterations instead of an expected log 2 (N) 1 iterations. On the other hand, this implementation makes fewer comparisons: log 2 (N) is less than the expected number of comparisons for the twotest implementations of 1 5(log 2 (N) 1), for N greater than eight. Language support Many standard libraries provide a way to do a binary search: C provides bsearch(3) ( in its standard library. C++'s STL provides algorithm functions binary_search, lower_bound and upper_bound. Java offers a set of overloaded binarysearch() static methods in the classes Arrays ( and Collections ( /util/collections.html) in the standard java.util package for performing binary searches on Java arrays and on collections with indexed random access, respectively. They must be arrays of
11 11 of 12 9/8/2010 6:37 PM primitives, or the arrays or Lists must be of a type that implements the Comparable interface, or you must specify a custom Comparator object. Microsoft's.NET Framework 2.0 offers static generic versions of the Binary Search algorithm in its collection base classes. An example would be System.Array's method BinarySearch<T>(T[] array, T value). Python provides the bisect ( module. COBOL can perform binary search on internal tables using the SEARCH ALL statement. Perl can perform a generic binary search using the CPAN module Search::Binary ( /perldoc?search::binary). See also Index (information technology) Very fast 'lookup' using an index to directly select an entry Branch tables Alternative indexed 'lookup' technique for decision making Selfbalancing binary search tree Runtime analysis, illustrating binary search technique on machines of differing speeds Bisection method, the same idea used to solve equations in the real numbers References ^ Introduction to Algorithms, [1] ( pp15 ^ ^ Bentley, Jon (2000) [1986]. Programming Pearls (2nd edition ed.). AddisonWesley. p. 34. ISBN ^ Extra, Extra Read All About It: Nearly All Binary Searches and Mergesorts are Broken ( Google Research Blog ^ Bug ID: (coll) binarysearch() fails for size larger than 1<<30 ( /view_bug.do?bug_id= ) ^ [2] ( ^ Niklaus Wirth: Algorithms + Data Structures = Programs. PrenticeHall, 1975, ISBN [3] ( Donald Knuth. The Art of Computer Programming, Volume 3: Sorting and Searching, Third Edition. AddisonWesley, ISBN Section 6.2.1: Searching an Ordered Table, pp Kruse, Robert L.: "Data Structures and Program Design in C++", PrenticeHall, 1999, ISBN , page 280. Netty van Gasteren, Wim Feijen. The Binary Search Revisited ( /wf214.pdf), AvG127/WF214, (investigates the foundations of the Binary Search, debunking the myth that it applies only to sorted arrays) External links NIST Dictionary of Algorithms and Data Structures: binary search ( /binarysearch.html) Google Research: Nearly All Binary Searches and Mergesorts are Broken ( Binary search implemented in 12 languages ( Retrieved from "" Categories: Search algorithms
12 12 of 12 9/8/2010 6:37 PM This page was last modified on 4 September 2010 at 02:52. Text is available under the Creative Commons AttributionShareAlike License; additional terms may apply. See Terms of Use for details. Wikipedia is a registered trademark of the Wikimedia Foundation, Inc., a nonprofit organization.
Chapter 5 Hashing. Introduction. Hashing. Hashing Functions. hashing performs basic operations, such as insertion,
Introduction Chapter 5 Hashing hashing performs basic operations, such as insertion, deletion, and finds in average time 2 Hashing a hash table is merely an of some fixed size hashing converts into locations
More informationIntroduction. hashing performs basic operations, such as insertion, better than other ADTs we ve seen so far
Chapter 5 Hashing 2 Introduction hashing performs basic operations, such as insertion, deletion, and finds in average time better than other ADTs we ve seen so far 3 Hashing a hash table is merely an hashing
More informationDESIGN AND ANALYSIS OF ALGORITHMS. Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES
DESIGN AND ANALYSIS OF ALGORITHMS Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES http://milanvachhani.blogspot.in USE OF LOOPS As we break down algorithm into subalgorithms, sooner or later we shall
More informationFundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.
Fundamental mathematical techniques reviewed: Mathematical induction Recursion Typically taught in courses such as Calculus and Discrete Mathematics. Techniques introduced: DivideandConquer Algorithms
More informationLogic, Words, and Integers
Computer Science 52 Logic, Words, and Integers 1 Words and Data The basic unit of information in a computer is the bit; it is simply a quantity that takes one of two values, 0 or 1. A sequence of k bits
More informationLecture Notes for Chapter 2: Getting Started
Instant download and all chapters Instructor's Manual Introduction To Algorithms 2nd Edition Thomas H. Cormen, Clara Lee, Erica Lin https://testbankdata.com/download/instructorsmanualintroductionalgorithms2ndeditionthomashcormenclaraleeericalin/
More informationMergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri
CS161, Lecture 2 MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri 1 Introduction Today, we will introduce a fundamental algorithm design paradigm,
More informationOutline: Search and Recursion (Ch13)
Search and Recursion Michael Mandel Lecture 12 Methods in Computational Linguistics I The City University of New York, Graduate Center https://github.com/ling78100/lectureexamples/blob/master/lecture12final.ipynb
More informationData Representation Type of Data Representation Integers Bits Unsigned 2 s Comp Excess 7 Excess 8
Data Representation At its most basic level, all digital information must reduce to 0s and 1s, which can be discussed as binary, octal, or hex data. There s no practical limit on how it can be interpreted
More information6/4/12. Recursive void Methods. Chapter 11. Vertical Numbers. Vertical Numbers. Vertical Numbers. Algorithm for Vertical Numbers
Recursive void Methods Chapter 11 Recursion Slides prepared by Rose Williams, Binghamton University A recursive method is a method that includes a call to itself Recursion is based on the general problem
More informationMerge Sort Roberto Hibbler Dept. of Computer Science Florida Institute of Technology Melbourne, FL
Merge Sort Roberto Hibbler Dept. of Computer Science Florida Institute of Technology Melbourne, FL 32901 rhibbler@cs.fit.edu ABSTRACT Given an array of elements, we want to arrange those elements into
More informationLecture 2: Getting Started
Lecture 2: Getting Started Insertion Sort Our first algorithm is Insertion Sort Solves the sorting problem Input: A sequence of n numbers a 1, a 2,..., a n. Output: A permutation (reordering) a 1, a 2,...,
More informationDual Sorting Algorithm Based on Quick Sort
Dual ing Algorithm Based on Quick 1 P. Dhivakar, 2 G. Jayaprakash 1 PG Student, 2 PG Student, Department of CSE M. Kumarasamy College of Engineering (Autonomous), Karur, TamilNadu, India dhivakarit92@gmail.com
More informationSearching Algorithms/Time Analysis
Searching Algorithms/Time Analysis CSE21 Fall 2017, Day 8 Oct 16, 2017 https://sites.google.com/a/eng.ucsd.edu/cse21fall2017milesjones/ (MinSort) loop invariant induction Loop invariant: After the
More informationCSCI1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion
CSCI1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion Review from Lectures 5 & 6 Arrays and pointers, Pointer arithmetic and dereferencing, Types of memory ( automatic, static,
More informationAlgorithm Design and Recursion. Search and Sort Algorithms
Algorithm Design and Recursion Search and Sort Algorithms Objectives To understand the basic techniques for analyzing the efficiency of algorithms. To know what searching is and understand the algorithms
More informationLecture Notes on Quicksort
Lecture Notes on Quicksort 15122: Principles of Imperative Computation Frank Pfenning Lecture 8 February 5, 2015 1 Introduction In this lecture we consider two related algorithms for sorting that achieve
More informationLecture. Algorithm Design and Recursion. Richard E Sarkis CSC 161: The Art of Programming
Lecture Algorithm Design and Recursion Richard E Sarkis CSC 161: The Art of Programming Class Administrivia Objectives To understand the basic techniques for analyzing the efficiency of algorithms To know
More informationCS261 Data Structures. Ordered Array Dynamic Array Implementation
CS261 Data Structures Ordered Array Dynamic Array Implementation Recall Arrays Operations Add O(1+) for dynamic array Contains O(n) Remove O(n) What if our application needs to repeatedly call Contains
More informationChapter Fourteen Bonus Lessons: Algorithms and Efficiency
: Algorithms and Efficiency The following lessons take a deeper look at Chapter 14 topics regarding algorithms, efficiency, and Big O measurements. They can be completed by AP students after Chapter 14.
More informationCSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. DecreaseandConquer
CSC 8301 Design and Analysis of Algorithms Lecture 5 Decrease and Conquer Algorithm Design Technique DecreaseandConquer This algorithm design technique is based on exploiting a relationship between
More informationCOMP6700/2140 Recursive Operations
COMP6700/2140 Recursive Operations Alexei B Khorev and Josh Milthorpe Research School of Computer Science, ANU May 2017 Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Recursive Operations
More informationSorting is a problem for which we can prove a nontrivial lower bound.
Sorting The sorting problem is defined as follows: Sorting: Given a list a with n elements possessing a total order, return a list with the same elements in nondecreasing order. Remember that total order
More informationComputer Science 210 Data Structures Siena College Fall Topic Notes: Searching and Sorting
Computer Science 10 Data Structures Siena College Fall 016 Topic Notes: Searching and Sorting Searching We all know what searching is looking for something. In a computer program, the search could be:
More informationLecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning
Lecture 7 Quicksort 15122: Principles of Imperative Computation (Spring 2018) Frank Pfenning In this lecture we consider two related algorithms for sorting that achieve a much better running time than
More informationProblem with Scanning an Infix Expression
Operator Notation Consider the infix expression (X Y) + (W U), with parentheses added to make the evaluation order perfectly obvious. This is an arithmetic expression written in standard form, called infix
More informationLecture 15 Binary Search Trees
Lecture 15 Binary Search Trees 15122: Principles of Imperative Computation (Fall 2017) Frank Pfenning, André Platzer, Rob Simmons, Iliano Cervesato In this lecture, we will continue considering ways to
More informationMergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015
CS161, Lecture 2 MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015 1 Introduction Today, we will introduce a fundamental algorithm design paradigm, DivideAndConquer,
More informationSummer Final Exam Review Session August 5, 2009
15111 Summer 2 2009 Final Exam Review Session August 5, 2009 Exam Notes The exam is from 10:30 to 1:30 PM in Wean Hall 5419A. The exam will be primarily conceptual. The major emphasis is on understanding
More informationComparing Implementations of Optimal Binary Search Trees
Introduction Comparing Implementations of Optimal Binary Search Trees Corianna Jacoby and Alex King Tufts University May 2017 In this paper we sought to put together a practical comparison of the optimality
More informationType Checking and Type Equality
Type Checking and Type Equality Type systems are the biggest point of variation across programming languages. Even languages that look similar are often greatly different when it comes to their type systems.
More informationData Structures Lecture 3 Order Notation and Recursion
Data Structures Lecture 3 Order Notation and Recursion 1 Overview The median grade.cpp program from Lecture 2 and background on constructing and using vectors. Algorithm analysis; order notation Recursion
More informationSome Applications of Graph Bandwidth to Constraint Satisfaction Problems
Some Applications of Graph Bandwidth to Constraint Satisfaction Problems Ramin Zabih Computer Science Department Stanford University Stanford, California 94305 Abstract Bandwidth is a fundamental concept
More informationLecture 15 : Review DRAFT
CS/Math 240: Introduction to Discrete Mathematics 3/10/2011 Lecture 15 : Review Instructor: Dieter van Melkebeek Scribe: Dalibor Zelený DRAFT Today slectureservesasareviewofthematerialthatwillappearonyoursecondmidtermexam.
More informationCS301  Data Structures Glossary By
CS301  Data Structures Glossary By Abstract Data Type : A set of data values and associated operations that are precisely specified independent of any particular implementation. Also known as ADT Algorithm
More informationKochLike Fractal Images
Bridges Finland Conference Proceedings KochLike Fractal Images Vincent J. Matsko Department of Mathematics University of San Francisco vince.matsko@gmail.com Abstract The Koch snowflake is defined by
More informationAlgorithms and Applications
Algorithms and Applications 1 Areas done in textbook: Sorting Algorithms Numerical Algorithms Image Processing Searching and Optimization 2 Chapter 10 Sorting Algorithms  rearranging a list of numbers
More informationRadix Searching. The insert procedure for digital search trees also derives directly from the corresponding procedure for binary search trees:
Radix Searching The most simple radix search method is digital tree searching  the binary search tree with the branch in the tree according to the bits of keys: at the first level the leading bit is used,
More informationSorting & Growth of Functions
Sorting & Growth of Functions CSci 588: Data Structures, Algorithms and Software Design Introduction to Algorithms, Cormen et al., Chapter 3 All material not from online sources or text copyright Travis
More informationLIMITATIONS OF COMPUTING. Introduction to Computer Engineering 2015 Spring by Euiseong Seo
LIMITATIONS OF COMPUTING Introduction to Computer Engineering 2015 Spring by Euiseong Seo Where are we? Chapter 1: The Big Picture Chapter 2: Binary Values and Number Systems Chapter 3: Date Representation
More informationWe assume uniform hashing (UH):
We assume uniform hashing (UH): the probe sequence of each key is equally likely to be any of the! permutations of 0,1,, 1 UH generalizes the notion of SUH that produces not just a single number, but a
More informationChapter 12: Indexing and Hashing
Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B+Tree Index Files BTree Index Files Static Hashing Dynamic Hashing Comparison of Ordered Indexing and Hashing Index Definition in SQL
More informationAlgorithm Efficiency & Sorting. Algorithm efficiency BigO notation Searching algorithms Sorting algorithms
Algorithm Efficiency & Sorting Algorithm efficiency BigO notation Searching algorithms Sorting algorithms Overview Writing programs to solve problem consists of a large number of decisions how to represent
More informationData Structures. Topic #6
Data Structures Topic #6 Today s Agenda Table Abstract Data Types Work by value rather than position May be implemented using a variety of data structures such as arrays (statically, dynamically allocated)
More informationCSE 214 Computer Science II Searching
CSE 214 Computer Science II Searching Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse214/sec02/ Introduction Searching in a
More informationTable : IEEE Single Format ± a a 2 a 3 :::a 8 b b 2 b 3 :::b 23 If exponent bitstring a :::a 8 is Then numerical value represented is ( ) 2 = (
Floating Point Numbers in Java by Michael L. Overton Virtually all modern computers follow the IEEE 2 floating point standard in their representation of floating point numbers. The Java programming language
More informationData Structures and Algorithms
Data Structures and Algorithms About the course (objectives, outline, recommended reading) Problem solving Notions of Algorithmics (growth of functions, efficiency, programming model, example analysis)
More informationDesign and Analysis of Algorithms. Comp 271. Mordecai Golin. Department of Computer Science, HKUST
Design and Analysis of Algorithms Revised 05/02/03 Comp 271 Mordecai Golin Department of Computer Science, HKUST Information about the Lecturer Dr. Mordecai Golin Office: 3559 Email: golin@cs.ust.hk http://www.cs.ust.hk/
More informationSorting: Given a list A with n elements possessing a total order, return a list with the same elements in nondecreasing order.
Sorting The sorting problem is defined as follows: Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in nondecreasing order. Remember that total order
More informationAlgorithms + Data Structures Programs
Introduction Algorithms Method for solving problems suitable for computer implementation Generally independent of computer hardware characteristics Possibly suitable for many different programming languages
More information(Refer Slide Time 04:53)
Programming and Data Structure Dr.P.P.Chakraborty Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture 26 Algorithm Design 1 Having made a preliminary study
More information8 Algorithms 8.1. Foundations of Computer Science Cengage Learning
8 Algorithms 8.1 Foundations of Computer Science Cengage Learning 8.2 Objectives After studying this chapter, the student should be able to: Define an algorithm and relate it to problem solving. Define
More informationThe Encoding Complexity of Network Coding
The Encoding Complexity of Network Coding Michael Langberg Alexander Sprintson Jehoshua Bruck California Institute of Technology Email: mikel,spalex,bruck @caltech.edu Abstract In the multicast network
More informationIntroduction to Asymptotic Running Time Analysis CMPSC 122
Introduction to Asymptotic Running Time Analysis CMPSC 122 I. Comparing Two Searching Algorithms Let's begin by considering two searching algorithms you know, both of which will have input parameters of
More informationComputational Geometry
Windowing queries Windowing Windowing queries Zoom in; recenter and zoom in; select by outlining Windowing Windowing queries Windowing Windowing queries Given a set of n axisparallel line segments, preprocess
More informationIntroduction to Computers & Programming
16.070 Introduction to Computers & Programming Ada: Recursion Prof. Kristina Lundqvist Dept. of Aero/Astro, MIT Recursion Recursion means writing procedures and functions which call themselves. Recursion
More informationReals 1. Floatingpoint numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method.
Reals 1 13 Reals Floatingpoint numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method. 13.1 Floatingpoint numbers Real numbers, those declared to be
More informationExcerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.
Chapter 5 Using the Integers In spite of their being a rather restricted class of numbers, the integers have a lot of interesting properties and uses. Math which involves the properties of integers is
More informationn 1 x i = xn 1 x 1. i= (2n 1) = n 2.
Questions 1. Use mathematical induction to prove that, for any n 1 n 1 x i = xn 1 x 1. (Recall that I proved it in a different way back in lecture 2.) 2. Use mathematical induction to prove that, for all
More informationPrinciples of Algorithm Design
Principles of Algorithm Design When you are trying to design an algorithm or a data structure, it s often hard to see how to accomplish the task. The following techniques can often be useful: 1. Experiment
More informationRecursion. Chapter 11
Walter Savitch Frank M. Carrano Recursion Chapter 11 ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Objectives Describe the concept of recursion Use recursion
More informationClassic Data Structures Introduction UNIT I
ALGORITHM SPECIFICATION An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. All algorithms must satisfy the following criteria: Input. An algorithm has zero
More informationChapter 11: Indexing and Hashing
Chapter 11: Indexing and Hashing Basic Concepts Ordered Indices B + Tree Index Files BTree Index Files Static Hashing Dynamic Hashing Comparison of Ordered Indexing and Hashing Index Definition in SQL
More informationChapter 8 Algorithms 1
Chapter 8 Algorithms 1 Objectives After studying this chapter, the student should be able to: Define an algorithm and relate it to problem solving. Define three construct and describe their use in algorithms.
More informationCS Data Structures and Algorithm Analysis
CS 483  Data Structures and Algorithm Analysis Lecture VI: Chapter 5, part 2; Chapter 6, part 1 R. Paul Wiegand George Mason University, Department of Computer Science March 8, 2006 Outline 1 Topological
More informationLecture 10: Introduction to Correctness
Lecture 10: Introduction to Correctness Aims: To look at the different types of errors that programs can contain; To look at how we might detect each of these errors; To look at the difficulty of detecting
More informationCS60003 Algorithm Design and Analysis, Autumn
CS60003 Algorithm Design and Analysis, Autumn 2009 10 MidSemester Test Maximum marks: 45 September 20, 2009 Total time: 2 hours Roll no: Name: [ Write your answers in the question paper itself. Be brief
More informationCS:3330 (22c:31) Algorithms
What s an Algorithm? CS:3330 (22c:31) Algorithms Introduction Computer Science is about problem solving using computers. Software is a solution to some problems. Algorithm is a design inside a software.
More informationChapter 11: Indexing and Hashing
Chapter 11: Indexing and Hashing Database System Concepts, 6 th Ed. See www.dbbook.com for conditions on reuse Chapter 11: Indexing and Hashing Basic Concepts Ordered Indices B + Tree Index Files BTree
More informationConstructing Algorithms and Pseudocoding This document was originally developed by Professor John P. Russo
Constructing Algorithms and Pseudocoding This document was originally developed by Professor John P. Russo Purpose: # Describe the method for constructing algorithms. # Describe an informal language for
More informationChapter 12: Indexing and Hashing
Chapter 12: Indexing and Hashing Database System Concepts, 5th Ed. See www.dbbook.com for conditions on reuse Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B + Tree Index Files BTree
More informationA NelderMead Tuner for Svm
A NelderMead Tuner for Svm prepared by: Kester Smith approved by: reference: issue: 1 revision: 0 date: 20090313 status: Draft Abstract Read at your own risk, as this is a working document and has not
More informationGateway Regional School District VERTICAL ARTICULATION OF MATHEMATICS STANDARDS Grades K4
NUMBER SENSE & OPERATIONS K.N.1 Count by ones to at least 20. When you count, the last number word you say tells the number of items in the set. Counting a set of objects in a different order does not
More informationAdvanced Algorithms Class Notes for Monday, October 23, 2012 Min Ye, Mingfu Shao, and Bernard Moret
Advanced Algorithms Class Notes for Monday, October 23, 2012 Min Ye, Mingfu Shao, and Bernard Moret Greedy Algorithms (continued) The best known application where the greedy algorithm is optimal is surely
More informationScan and its Uses. 1 Scan. 1.1 Contraction CSE341T/CSE549T 09/17/2014. Lecture 8
CSE341T/CSE549T 09/17/2014 Lecture 8 Scan and its Uses 1 Scan Today, we start by learning a very useful primitive. First, lets start by thinking about what other primitives we have learned so far? The
More informationKey Concepts: Economic Computation, Part II
Key Concepts: Economic Computation, Part II Brent Hickman Fall, 2009 The purpose of the second section of these notes is to give you some further practice with numerical computation in MATLAB, and also
More informationChapter 3  Top Level View of Computer Function
Chapter 3  Top Level View of Computer Function Luis Tarrataca luis.tarrataca@gmail.com CEFETRJ L. Tarrataca Chapter 3  Top Level View 1 / 127 Table of Contents I 1 Introduction 2 Computer Components
More informationCOMP 122/L Lecture 2. Kyle Dewey
COMP 122/L Lecture 2 Kyle Dewey Outline Operations on binary values AND, OR, XOR, NOT Bit shifting (left, two forms of right) Addition Subtraction Twos complement Bitwise Operations Bitwise AND Similar
More informationHomework. Reading: Chapter 17 Homework: All exercises from Chapter 17 Due: 10/27 Correction: Chapter 16 homework is due 10/25
Homework Reading: Chapter 17 Homework: All exercises from Chapter 17 Due: 10/27 Correction: Chapter 16 homework is due 10/25 Copyright c 2002 2017 UMaine Computer Science Department 1 / 33 1 COS 140: Foundations
More informationUNIT 5B Binary Search
205/09/30 UNIT 5B Binary Search Course Announcements Written exam next week (Wed. Oct 7 ) Practice exam available on the Resources page Exam reviews: Sunday afternoon; watch Piazza for times and places
More informationMeasuring Input size. Last lecture recap.
Measuring Input size Last lecture recap. Linear data structures Compiled from http://www.eagle.tamut.edu/faculty/igor/cis305.htm Abstract Data Type Data Structures. Main Notions and Definitions. Data
More informationTable ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum
Table ADT and Sorting Algorithm topics continuing (or reviewing?) CS 24 curriculum A table ADT (a.k.a. Dictionary, Map) Table public interface: // Put information in the table, and a unique key to identify
More information1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors
1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors on an EREW PRAM: See solution for the next problem. Omit the step where each processor sequentially computes the AND of
More informationSTA Rev. F Learning Objectives. Learning Objectives (Cont.) Module 3 Descriptive Measures
STA 2023 Module 3 Descriptive Measures Learning Objectives Upon completing this module, you should be able to: 1. Explain the purpose of a measure of center. 2. Obtain and interpret the mean, median, and
More informationLecture 16. Reading: Weiss Ch. 5 CSE 100, UCSD: LEC 16. Page 1 of 40
Lecture 16 Hashing Hash table and hash function design Hash functions for integers and strings Collision resolution strategies: linear probing, double hashing, random hashing, separate chaining Hash table
More informationRun Times. Efficiency Issues. Run Times cont d. More on O( ) notation
Comp2711 S1 2006 Correctness Oheads 1 Efficiency Issues Comp2711 S1 2006 Correctness Oheads 2 Run Times An implementation may be correct with respect to the Specification Pre and Postcondition, but nevertheless
More information6 Distributed data management I Hashing
6 Distributed data management I Hashing There are two major approaches for the management of data in distributed systems: hashing and caching. The hashing approach tries to minimize the use of communication
More informationAlgorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs
Algorithms in Systems Engineering IE172 Midterm Review Dr. Ted Ralphs IE172 Midterm Review 1 Textbook Sections Covered on Midterm Chapters 15 IE172 Review: Algorithms and Programming 2 Introduction to
More informationDatabase System Concepts, 6 th Ed. Silberschatz, Korth and Sudarshan See for conditions on reuse
Chapter 11: Indexing and Hashing Database System Concepts, 6 th Ed. See www.dbbook.com for conditions on reuse Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B + Tree Index Files Static
More informationOutline for Today. How can we speed up operations that work on integer data? A simple data structure for ordered dictionaries.
van Emde Boas Trees Outline for Today Data Structures on Integers How can we speed up operations that work on integer data? Tiered Bitvectors A simple data structure for ordered dictionaries. van Emde
More informationStatistics Case Study 2000 M. J. Clancy and M. C. Linn
Statistics Case Study 2000 M. J. Clancy and M. C. Linn Problem Write and test functions to compute the following statistics for a nonempty list of numeric values: The mean, or average value, is computed
More informationBalanced Search Trees
Balanced Search Trees Computer Science E22 Harvard Extension School David G. Sullivan, Ph.D. Review: Balanced Trees A tree is balanced if, for each node, the node s subtrees have the same height or have
More informationUnit 8: Analysis of Algorithms 1: Searching
P Computer Science Unit 8: nalysis of lgorithms 1: Searching Topics: I. Sigma and BigO notation II. Linear Search III. Binary Search Materials: I. Rawlins 1.6 II. Rawlins 2.1 III. Rawlins 2.3 IV. Sigma
More informationECE 242 Data Structures and Algorithms. Advanced Sorting I. Lecture 16. Prof.
ECE 242 Data Structures and Algorithms http://www.ecs.umass.edu/~polizzi/teaching/ece242/ Advanced Sorting I Lecture 16 Prof. Eric Polizzi Sorting Algorithms... so far Bubble Sort Selection Sort Insertion
More informationHASH TABLES. Hash Tables Page 1
HASH TABLES TABLE OF CONTENTS 1. Introduction to Hashing 2. Java Implementation of Linear Probing 3. Maurer s Quadratic Probing 4. Double Hashing 5. Separate Chaining 6. Hash Functions 7. Alphanumeric
More informationDivisibility Rules and Their Explanations
Divisibility Rules and Their Explanations Increase Your Number Sense These divisibility rules apply to determining the divisibility of a positive integer (1, 2, 3, ) by another positive integer or 0 (although
More informationData Structure. Measuring Input size. Composite Data Structures. Linear data structures. Data Structure is: Abstract Data Type 1/9/2014
Data Structure Measuring Input size Last lecture recap. A Data Structure is an aggregation of atomic and composite data into a set with defined relationships. Structure means a set of rules that holds
More informationSorting. Order in the court! sorting 1
Sorting Order in the court! sorting 1 Importance of sorting Sorting a list of values is a fundamental task of computers  this task is one of the primary reasons why people use computers in the first place
More informationRoundoff Errors and Computer Arithmetic
Jim Lambers Math 105A Summer Session I 200304 Lecture 2 Notes These notes correspond to Section 1.2 in the text. Roundoff Errors and Computer Arithmetic In computing the solution to any mathematical problem,
More informationProblem Set 5 Solutions
Introduction to Algorithms November 4, 2005 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik D. Demaine and Charles E. Leiserson Handout 21 Problem Set 5 Solutions Problem 51. Skip
More information