Sequential Search and List Extremes: Principles of Computer Science Algorithm Discovery

Algorithm Discovery and Design Fundamentals\n\n- Algorithm Discovery: Finding a solution to a given problem. This is considered the most challenging and creative part of the problem-solving process.\n- Design Strategy: A common strategy for algorithm discovery is to determine how the same problem would be solved manually by hand. If a manual solution can be understood and explained, it can often be expressed as a formal algorithm.\n- Influence of Data Organization: A critical concept in algorithm design is that the selection of an algorithm to solve a problem is greatly influenced by the way the input data is organized (e.g., whether a list is sorted or unordered).\n- Algorithm Correctness: An algorithm is deemed correct only when it produces the correct result for all possible cases, including edge cases.\n- Efficiency and Elegance: Computer scientists aim for algorithms that are not only correct but also efficient (performing tasks in a reasonable amount of time or steps) and elegant (being well-structured and concise).\n\n# The Sequential Search Algorithm\n\n- Problem Definition (Reverse Telephone Lookup): Given a phone number (NUMBERNUMBER), find the name associated with it in a list of 10,00010,000 pairs of numbers (T1,T2,...,T10,000T_1, T_2, ..., T_{10,000}) and names (N1,N2,...,N10,000N_1, N_2, ..., N_{10,000}).\n- The Moving Finger Metaphor: The variable ii acts as an index or pointer into the list, resembling a \"moving finger\" scanning the telephone numbers and pointing to the one the algorithm is currently evaluating.\n- Inefficient First Attempt (Figure 2.11): Initial designs without iteration would require writing 10,00010,000 separate conditional statements. This would take roughly 150150 pages at 6666 lines per page, be highly inefficient (continuing to check after finding the answer), and would be incorrect if the number is missing from the list.\n- Refined Sequential Search (Figure 2.13):\n - Step 1: Get values for NUMBERNUMBER, T1,...,T10,000T_1, ..., T_{10,000}, and N1,...,N10,000N_1, ..., N_{10,000}.\n - Step 2: Set the value of ii to 11 and set the value of FoundFound to NONO.\n - Step 3: While both (Found=NOFound = NO) and (ile10,000i \\le 10,000) do Steps 4 through 7.\n - Step 4: If NUMBERNUMBER is equal to the ithi^{th} number on the list, TiT_i, then…\n - Step 5: …Print the name of the corresponding person, NiN_i.\n - Step 6: …Set the value of FoundFound to YESYES.\n - Step 7: Else (if NUMBERNUMBER is not equal to TiT_i), add 11 to the value of ii.\n - Step 8: If (Found=NOFound = NO) then…\n - Step 9: …Print the message 'Sorry, this number is not in the directory'.\n - Step 10: Stop.\n- Scalability: If the phone book contained 1,000,0001,000,000 or even 1,000,000,0001,000,000,000 numbers, only two lines would require modification: those defining the input size (Line 1) and those defining the loop limit (Line 3).\n\n# Finding the Largest Value in a List\n\n- Problem Definition: Given a value nge1n \\ge 1 and a list of unique numbers A1,A2,...,AnA_1, A_2, ..., A_n, find and print the numerically largest value and its position (location) in the list.\n- Informal Strategy: Initially assume the first item is the largest. Compare this against subsequent items. If a new item is larger, replace the stored maximum with this new value and update its recorded position. Discard values that are smaller.\n- Metaphorical Example: Imagine a pile of papers with numbers (19,41,12,63,2219, 41, 12, 63, 22). If the current \"largest so far\" is 1919 and the next is 4141, discard 1919 and keep 4141. If the next is 1212, discard it and keep 4141. This continues until the pile is empty.\n- Detailed Formal Algorithm (Figure 2.14):\n - Input: Get a value for nn (size of list) and values for A1,A2,...,AnA_1, A_2, ..., A_n.\n - Initialization: Set the value of largest so far\text{largest so far} to A1A_1. Set the value of location\text{location} to 11. Set the value of ii to 22.\n - Execution Loop: While (ileni \\le n) do:\n - If A_i > \\text{largest so far} then:\n - Set largest so far\text{largest so far} to AiA_i.\n - Set location\text{location} to ii.\n - Add 11 to the value of ii.\n - Termination: Print out the values of largest so far\text{largest so far} and location\text{location}. Stop.\n- Logic Note: The index ii starts at 22 because the first item (A1A_1) is already used as the initial baseline. The continuation condition (ileni \\le n) ensures the loop finishes after the entire list is searched.\n\n# Building Blocks and Libraries\n\n- Building-Block Concept: Once an algorithm is developed, it can be used as a primitive operation in constructing more complex algorithms. For example, the Find Largest algorithm is a building block for sorting an unordered list into ascending order.\n- Libraries: These are collections of useful, prewritten algorithms. They are essential tools for algorithm design because they allow developers to build on existing results rather than starting from absolute basic primitives (sequential, conditional, iterative) every time.\n\n# Multiplication via Repeated Addition\n\n- Basic Concept: Multiplication of two non-negative integers aa and bb can be achieved by adding the value of aa to a running total bb times.\n- Initial Multiplication Algorithm (Figure 2.10):\n - Get values for aa and bb.\n - If (either a=0a = 0 or b=0b = 0) then set productproduct to 00.\n - Else, set countcount to 00 and productproduct to 00.\n - While (count < b) do:\n - Set productproduct to (product+aproduct + a).\n - Set countcount to (count+1count + 1).\n - Print the value of productproduct and Stop.\n- Efficiency Fixes: Without the initial check for a=0a = 0 or b=0b = 0, the algorithm would still find the correct answer of 00, but it might do so through thousands of unnecessary additions (e.g., adding 00 to a sum 5,3865,386 times if b=5,386b = 5,386).\n- Handling Negative Numbers: The standard algorithm fails if b < 0 because the condition (count < b) is immediately false. To fix this:\n - Set a boolean flag bnegative\text{bnegative} to NONO.\n - If b < 0, set b=bb = -b and set bnegative\text{bnegative} to YESYES.\n - Calculate the product using the absolute value of bb.\n - If bnegative=YES\text{bnegative} = YES, the final result is product-product.\n- Algorithm Limitation Warning: The text suggests adding a check: if a > 10,000 or b > 10,000, display a message suggesting the use of a more efficient multiplication algorithm (like the traditional grade school method) instead of repeated addition.\n\n# Questions & Discussion\n\n- Q: What parts of the sequential search of Figure 2.13 change if the list is 1 million vs 10,000?\n- A: Only Line 1 (to input 1 million names/numbers) and Line 3 (to change the loop limit to 1 million) would need to be changed.\n\n- Q: How does the choice of data organization influence search?\n- A: Humans don't turn every page for a phone number because they exploit the numerical or alphabetical order. In a sorted reverse directory, one can start in the middle based on the first digit and move quickly, which is much faster than sequential search.\n\n- Q: Is repeated addition efficient for multiplication?\n- A: No, it is highly inefficient compared to grade-school multiplication because of the sheer number of operations required (e.g., adding 234234 to a total 567567 times).", "title": "Sequential Search and List Extremes: Principles of Computer Science Algorithm Discovery"}