By the end of this chapter, you will be able to:
Mastering these search skills will help you work smarter and faster in any tech-related task or project.
Searching is the process of finding whether a particular value, called the key or target, is present in a collection of data, and if so, where it is located. A search algorithm is the step-by-step procedure used to do this. Searching is one of the most common operations in computing: every login, database lookup, spell check and phone contact search depends on it, so the choice of search algorithm strongly affects how fast a program runs.
Example: an M-Pesa agent in Ugunja who looks up a customer's transaction by its reference code is performing a search; the agent's software must find one record among thousands in a fraction of a second.
Efficiency is expressed with Big-O notation, which describes how the number of comparisons grows with the number of elements n.
| Algorithm | Best case | Average case | Worst case | Requires sorted data |
|---|---|---|---|---|
| Linear search | O(1) | O(n) | O(n) | No |
| Binary search | O(1) | O(log n) | O(log n) | Yes |
| Hash table lookup | O(1) | O(1) | O(n) | No |
Create a free account to open more of this chapter.
Free: practical guides, quick cards, workplace scenarios and more.
Create a free accountType: Individual
| Tools & Equipment | Materials |
|---|---|
| Working computer | Two plain papers |
| C++ IDE |
| S/N | Item | Quantity |
|---|---|---|
| 1 | Working computer | 1 Pc per Candidate |
| 2 | C++ IDE | 1 Pc per Candidate |
| 3 | Two plain papers | 2 Pcs per Candidate |
| Items to be Evaluated | Marks Available | Marks Obtained | Comments |
|---|---|---|---|
| TASK 1: Define Search in Algorithms | |||
| Observed computer laboratory rules (Award 2 marks for compliance, or zero) | 2 | ||
| Wrote a clear and correct definition of search in algorithms on paper (Award 5 marks for a complete and accurate definition, or zero) | 5 | ||
| Sub-Total | 7 | ||
| TASK 2: Write Code Snippet for Linear Search | |||
| Launched C++ IDE correctly (Award 2 marks for successful launch, or zero) | 2 | ||
| Declared and initialized an integer array of size 10 (Award 2 marks for correct declaration and initialization, or zero) | 2 | ||
| Implemented linear search function with correct parameters (Award 5 marks for correct function implementation, or zero) | 5 | ||
| Correctly searched for a target integer value in the array (Award 4 marks for correct search logic, or zero) | 4 | ||
| Displayed appropriate message if element found or not found (Award 3 marks for correct messaging, or zero) | 3 | ||
| Compiled, debugged, and executed program successfully (Award 3 marks for successful execution, or zero) | 3 | ||
| Sub-Total | 19 | ||
| PRODUCT CHECKLIST | |||
| Definition of search is clear, concise, and accurate (Award 5 marks for an accurate and well-written definition, or zero) | 5 | ||
| Code snippet compiles and runs correctly, performing linear search as specified (Award 10 marks for a working, correct program, or zero) | 10 | ||
| Sub-Total | 15 | ||
| GRAND TOTAL | 41 | ||
Type: Individual
| Tools & Equipment | Materials |
|---|---|
| Working computer | Plain paper |
| C++ Integrated Development Environment (IDE) |
| S/N | Item | Quantity |
|---|---|---|
| 1 | Working computer | 1 Pc per Candidate |
| 2 | C++ Integrated Development Environment (IDE) | 1 Pc per Candidate |
| 3 | Plain paper | 2 Pcs per Candidate |
| Items to be Evaluated | Marks Available | Marks Obtained | Comments |
|---|---|---|---|
| TASK 1: Implement Linear Search Program | |||
| Observed computer laboratory rules (Award 2 marks for observing rules or zero) | 2 | ||
| Launched the C++ IDE correctly (Award 1 mark for successful launch or zero) | 1 | ||
| Declared and initialized an integer array of size 10 (Award 2 marks for correct declaration and initialization or zero) | 2 | ||
| Implemented a function for linear search with appropriate parameters (Award 2 marks for function name, 3 marks for correct parameter use and return type or zero) | 5 | ||
| Used a loop to iterate through the array elements correctly (Award 3 marks for correct loop structure or zero) | 3 | ||
| Compared each element with the target value within the loop (Award 2 marks for correct comparison or zero) | 2 | ||
| Returned the index of the found element or -1 if not found (Award 3 marks for correct return logic or zero) | 3 | ||
| Displayed appropriate messages for element found or not found (Award 3 marks for correct and clear console output or zero) | 3 | ||
| Commented the code adequately explaining key sections (Award 1 mark for each relevant comment, max 2 marks) | 2 | ||
| Compiled, debugged, and executed the program successfully (Award 2 marks for successful execution or zero) | 2 | ||
| Sub-Total | 25 | ||
| PRODUCT CHECKLIST | |||
| Program correctly finds and returns index of target element in array of 10 integers (Award 10 marks for correct functionality matching requirements or zero) | 10 | ||
| Sub-Total | 10 | ||
| GRAND TOTAL | 35 | ||
At the start of this chapter we promised you would be able to:
Tick each one you can genuinely do.
So, are you there yet?
You're competent when you can confidently do 50% or more of what this chapter promised.
Sign in to record how you're doing.