Chapter nine, algorithm and problem solving notes
Chapter nine, algorithm and problem solving notes
Solving
1⃣ Key Concepts & Definitions
🔹 What is an Algorithm?
🔹 Problem-Solving Techniques
Stepwise Refinement – Start with a broad solution and break it down into smaller
steps.
Divide and Conquer – Break the problem into subproblems, solve them, and
combine results.
Greedy Algorithm – Always pick the best choice at each step (e.g., shortest path in a
graph).
Backtracking – Try different solutions and undo steps if needed (e.g., solving a
maze).
Brute Force – Try all possible solutions (not efficient for large problems).
1⃣ Searching Algorithms
Linear Search – Checks each element one by one. (O(n) complexity)
Binary Search – Works on sorted lists; repeatedly divides in half. (O(log n)
complexity)
2⃣ Sorting Algorithms
Bubble Sort – Repeatedly swaps adjacent elements if they are in the wrong order.
(O(n²) complexity)
Insertion Sort – Inserts each element into its correct position. (O(n²) complexity)
Merge Sort – Splits the array into halves, sorts, and merges. (O(n log n) complexity)
Quick Sort – Uses a pivot element to partition data. (O(n log n) complexity)
3⃣ Recursion
3⃣ Write an algorithm that takes a user’s age and prints whether they are a child (0-12),
teenager (13-19), or adult (20+).
4⃣ Given a list of numbers [3, 7, 1, 9, 4], show the steps of Bubble Sort to arrange them in
ascending order.
5⃣ Write pseudocode for Binary Search on a sorted list.
6⃣ What is the time complexity of Merge Sort and why is it better than Bubble Sort?
🔟 If a program takes O(n²) time, how does increasing input size n affect performance?
1⃣1⃣ Which sorting algorithm is best for small datasets and which one is better for large
datasets? Why?
1⃣2⃣ Write an algorithm to check if a string is a palindrome (reads the same forward and
backward).
🎯 A robot is placed in a grid and can only move right or down. Write an algorithm to
find the number of ways the robot can move from (0,0) to (m,n) in the grid.
✅ Example:
Problem: Creating a video game
Decomposed into:
🔹 Why is it useful?
🔹 2. Abstraction
✅ Definition:
✅ Example:
Think of Google Maps:
A map for drivers highlights roads and traffic but removes details like buildings.
A map for tourists shows landmarks but hides unnecessary details like small roads.
🔹 Why is it useful?
🔹 3. Pattern Recognition
✅ Definition:
Pattern recognition is identifying similarities or trends in problems to find efficient
solutions.
✅ Example:
🔹 Why is it useful?
1⃣ A shopping website needs a search feature. How would you decompose the problem?
2⃣ A banking system requires user authentication (login). Break it down into smaller tasks.
📌 Abstraction Questions
5⃣ A student grades system is similar to a hospital patient record system. What patterns do
they share?
6⃣ What pattern do you notice in binary search that makes it more efficient than linear
search?