Docsity

Introduction to Algorithmic Thinking and Its Applications — Module 3: Efficient Thinking, Exercises of Algorithms and Programming

Toggle sidebar
bg1
Contents
Subject: Introduction to Algorithmic Thinking and Its Applications
Module 3: Efficient Thinking - Sorting & Searching
Table of Contents:
1. Mind Map Overview
2. Introduction: Let’s Bring Order to Chaos!
3. Revision Notes
4. Practice Questions
5. Answer Keys & Explanations
6. Module 3: Final Thoughts & Next Steps
1. Mind Map Overview
For a visual summary of this module’s key concepts and their connections, please
refer to the mind map (link needed):
Mind Map
2. Introduction: Let’s Bring Order to Chaos!
Hi Future Problem Solvers! �
In Module 2, we learned how to store data using different structures. Now,
we’ll learn two fundamental operations we often perform on that data: Sorting
(arranging data in order) and Searching (finding specific data).
Imagine trying to find a specific word in a dictionary – it’s easy because the
words are sorted alphabetically! Or imagine trying to find the highest mark in
a list of unsorted student scores – you’d have to check every single one. Sorting
and searching are everywhere, from arranging contacts on your phone to how
search engines find relevant web pages.
In this module, we’ll explore some common algorithms for sorting data and ef-
ficient techniques for searching within it. Mastering these concepts is crucial
because efficiency becomes critical when dealing with the large datasets com-
mon in AI and Data Science. A slightly smarter algorithm can save enormous
amounts of time and computing power!
2.1 Audio Overview
For a quick recap, you can also check out this concise podcast:
Sorting and Searching Fundamentals.wav
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Introduction to Algorithmic Thinking and Its Applications — Module 3: Efficient Thinking and more Exercises Algorithms and Programming in PDF only on Docsity!

Contents

Subject: Introduction to Algorithmic Thinking and Its Applications Module 3: Efficient Thinking - Sorting & Searching

Table of Contents:

  1. Mind Map Overview
  2. Introduction: Let’s Bring Order to Chaos!
  3. Revision Notes
  4. Practice Questions
  5. Answer Keys & Explanations
  6. Module 3: Final Thoughts & Next Steps 1. Mind Map Overview For a visual summary of this module’s key concepts and their connections, please refer to the mind map (link needed): Mind Map 2. Introduction: Let’s Bring Order to Chaos! Hi Future Problem Solvers! ￿ In Module 2, we learned how to store data using different structures. Now, we’ll learn two fundamental operations we often perform on that data: Sorting (arranging data in order) and Searching (finding specific data).

Imagine trying to find a specific word in a dictionary – it’s easy because the words are sorted alphabetically! Or imagine trying to find the highest mark in a list of unsorted student scores – you’d have to check every single one. Sorting and searching are everywhere, from arranging contacts on your phone to how search engines find relevant web pages. In this module, we’ll explore some common algorithms for sorting data and ef- ficient techniques for searching within it. Mastering these concepts is crucial because efficiency becomes critical when dealing with the large datasets com- mon in AI and Data Science. A slightly smarter algorithm can save enormous amounts of time and computing power! 2.1 Audio Overview For a quick recap, you can also check out this concise podcast: Sorting and Searching Fundamentals.wav

2.2 How to Make the Most of These Revision Notes:

  • Trace the Steps: Sorting and searching algorithms involve steps. Try tracing them manually with small examples (like the ones provided) to truly understand how they work.
  • Code Connection: Look at the simple Python code snippets. Try to relate the code lines back to the steps described in the explanation. Even if you’re new to coding, focus on the logic flow.
  • Efficiency Matters: Pay attention to the why. Why is Binary Search faster than Linear Search? Why are some sorting algorithms better than others in certain situations?
  • Keywords: Learn the terminology: Sorting, Searching, Bubble Sort, Se- lection Sort, Insertion Sort, Linear Search, Binary Search, Time Complex- ity (basic idea).
  • Practice is Key: As always, attempt the MCQs, Subjective, and Ap- plication questions yourself before checking the answers. This reinforces your learning.
  • Review & Refine: Use the answers and explanations to correct your understanding and solidify the concepts. 3. Revision Notes

3.1 Section 1: Getting Things in Order - Common Sorting Algorithms

3.1.1 What is Sorting?

Sorting is the process of arranging items in a collection (like numbers in a list, names in a phone book, products on a website) into a specific order, such as:

  • Ascending Order: From smallest to largest (e.g., 1,2,3,4,5).
  • Descending Order: From largest to smallest (e.g., 5,4,3,2,1).
  • Alphabetical Order: (e.g., Apple, Banana, Cherry). Why Sort? Sorted data makes many other tasks much easier and faster, especially searching!

3.1.2 Bubble Sort: The Simple Swapper

  • Concept: Repeatedly steps through the list, compares adjacent pairs of elements, and swaps them if they are in the wrong order. The largest (or smallest) elements “bubble” up to their correct position. It keeps passing through the list until no swaps are needed, meaning it’s sorted.
  • Analogy: Imagine students lining up by height. You compare adjacent students; if the taller one is before the shorter one, they swap places. Repeat this pass several times until everyone is in height order.
  • How it Works (Ascending Order):
    1. Start at the beginning of the list.
    2. Compare the first element with the second. If the first > second, swap them.

complexity is O(n2).

3.1.3 Selection Sort: Finding the Minimum

  • Concept: Repeatedly finds the minimum element from the unsorted part of the list and puts it at the beginning of the sorted part.
  • Analogy: Imagine picking players for a cricket team based on batting average. You scan the entire list of available players, select the one with the highest average, and put them first. Then, you scan the remaining players, find the next highest, put them second, and so on.
  • How it Works (Ascending Order):
    1. Find the minimum element in the entire list.
    2. Swap it with the element at the first position.
    3. Now consider the list starting from the second position. Find the minimum element in this sub-list.
    4. Swap it with the element at the second position.
    5. Continue this process until the entire list is sorted.
  • Example Trace: Sort [5, 1, 4, 2] - Pass 1: ∗ Find minimum in [5, 1, 4, 2] → It’s 1 (at index 1). ∗ Swap minimum (1) with the first element (5) →[1,5,4,2] - Pass 2: (Consider [5, 4, 2]) ∗ Find minimum in [5, 4, 2] → It’s 2. ∗ Swap minimum (2) with the first element of sublist (5) →[1,2,4,5] - Pass 3: (Consider [4, 5]) ∗ Find minimum in [4, 5] → It’s 4. ∗ Swap minimum (4) with the first element of sublist (4) → No change →[1,2,4,5] - Sorted List: [1, 2, 4, 5]
  • Python Code Snippet:

Python

def selection_sort(arr): n = len(arr)

Traverse through all array elements

for i in range(n):

Find the minimum element in remaining unsorted array

min_idx = i for j in range(i+1, n): if arr[j] < arr[min_idx]: min_idx = j

Swap the found minimum element with the first element

arr[i], arr[min_idx] = arr[min_idx], arr[i] return arr

my_list = [5, 1, 4, 2] print("Selection Sorted:", selection_sort(my_list)) # Output: [1, 2, 4, 5]

  • Pros: Simple concept. Performs fewer swaps than Bubble Sort in many cases.
  • Cons: Still inefficient for large lists due to many comparisons. Time complexity is O(n2).

3.1.4 Insertion Sort: Building the Sorted List

  • Concept: Builds the final sorted list one item at a time. It takes each element from the unsorted input list and inserts it into its correct position within the already sorted part of the list.
  • Analogy: Arranging playing cards in your hand. You pick up one card at a time and insert it into the correct position relative to the cards you are already holding sorted.
  • How it Works (Ascending Order):
    1. Start with the second element (assume the first element is a sorted sub-list of size 1).
    2. Compare this element with the elements before it (in the sorted sub- list).
    3. Shift all elements in the sorted sub-list that are greater than the element being inserted one position to the right.
    4. Insert the element into the gap created.
    5. Take the next element from the unsorted part and repeat steps 2- until all elements are inserted.
  • Example Trace: Sort [5, 1, 4, 2] - Start with [5] (sorted), consider 1. - Compare 1 with 5. 5>1, so shift 5 right →[,5]. Insert 1→[1,5]. (Sorted part: [1, 5]) - Next element is 4. Compare 4 with 5. 5>4, shift 5 right →[1,,5]. Compare 4 with 1. 1<4, stop shifting. Insert 4→[1,4,5]. (Sorted part: [1, 4, 5]) - Next element is 2. Compare 2 with 5. 5>2, shift 5 right →[1,4, ,5]. Compare 2 with 4. 4>2, shift 4 right →[1, ,4,5]. Compare 2 with 1. 1<2, stop shifting. Insert 2→[1,2,4,5]. - Sorted List: [1, 2, 4, 5]
  • Python Code Snippet:

Python

def insertion_sort(arr):

Traverse through 1 to len(arr)

for i in range(1, len(arr)): key = arr[i] # Element to be inserted j = i-1 # Index of the last element in the sorted portion

Move elements of arr[0..i-1], that are greater than key,

to one position ahead of their current position

while j >= 0 and key < arr[j]: arr[j+1] = arr[j]

  • Analogy: Looking for a specific book on a completely disorganized book- shelf. You have to pick up and check each book individually until you find the one you want or check all the books.
  • How it Works:
    1. Start at the first element.
    2. Compare the current element with the target item.
    3. If they match, return the position (index) or confirm found.
    4. If they don’t match, move to the next element and repeat step 2.
    5. If you reach the end of the collection without finding the target, return “not found” or a specific indicator.
  • Example Trace: Search for 4 in [5, 1, 4, 2] - Compare 5 with 4→ No match. - Compare 1 with 4→ No match. - Compare 4 with 4→ Match found at index 2! Stop.
  • Python Code Snippet:

Python

def linear_search(arr, target): n = len(arr) for i in range(n): if arr[i] == target: return i # Target found at index i return -1 # Target not found

my_list = [5, 1, 4, 2] target_value = 4 index = linear_search(my_list, target_value) if index != -1: print(f"Linear Search: Target {target_value} found at index {index}") else: print(f"Linear Search: Target {target_value} not found")

Output: Linear Search: Target 4 found at index 2

  • Pros: Very simple to understand and implement. Works on unsorted data.
  • Cons: Very inefficient for large collections (worst case: have to check every item). Time complexity is O(n).

3.2.3 Binary Search: Divide and Conquer (Requires Sorted Data!)

  • Concept: A much faster searching algorithm that works only on SORTED collections. It repeatedly divides the search interval in half. It compares the target value with the middle element; if they don’t match, the half in which the target cannot lie is eliminated, and the search continues on the remaining half.
  • Analogy: Finding a word in a physical dictionary. You open it roughly in the middle. If your word comes alphabetically before the words on that

page, you know to only look in the first half of the dictionary. If it comes after, you only look in the second half. You repeat this process, narrowing down the search space very quickly.

  • How it Works (on a sorted list, e.g., ascending):
    1. Start with the entire sorted list (low index = 0, high index = last index).
    2. Find the middle index: mid=(low+high)//2.
    3. Compare the target value with the element at the middle index (arr[mid]). - If target == arr[mid] → Target found! Return mid. - If target < arr[mid] → The target must be in the left half. Repeat the search (step 2) using the left sub-list (new high = mid - 1). - If target > arr[mid] → The target must be in the right half. Repeat the search (step 2) using the right sub-list (new low = mid + 1).
    4. If low becomes greater than high, the search interval is empty, and the target is not in the list. Return “not found”.
  • Example Trace: Search for 4 in SORTED list [1, 2, 4, 5, 7] - low=0, high=4. mid=(0+4)//2=2. arr[2] is 4. - Compare target 4 with arr[2] (which is 4). They match! - Target found at index 2.
  • Example Trace: Search for 6 in SORTED list [1, 2, 4, 5, 7] - low=0, high=4. mid=(0+4)//2=2. arr[2] is 4. - Compare target 6 with arr[2] (which is 4). 6>4. Target must be in right half. - New search: low=mid+1=3, high=4. mid=(3+4)//2=3. arr[3] is 5. - Compare target 6 with arr[3] (which is 5). 6>5. Target must be in right half. - New search: low=mid+1=4, high=4$. $mid=(4+4)//2 = 4$.arr[4]‘ is 7. - Compare target 6 with arr[4] (which is 7 ). 6 < 7. Target must be in left half. - New search: ‘low=4, high=mid-1=3$. - Now low (4) > high (3). Search interval is empty. - Target 6 not found.
  • Python Code Snippet (Iterative Version):

Python

def binary_search(arr, target): low = 0 high = len(arr) - 1 mid = 0

while low <= high:

  1. Explain the core logic behind Insertion Sort. Use the list [6, 2, 4] and show the state of the list as each element (starting from the second) is inserted into the sorted portion. (4 Marks)
  2. Why is sorting data often a necessary first step before performing other tasks? Give one example task that becomes much easier or faster with sorted data. (3 Marks)
  3. Although Bubble, Selection, and Insertion Sort are generally inefficient (O(n2)), why are they often taught first in introductory courses? (2 Marks)

4.1.2 Multiple Choice Questions (MCQs) (1 Mark Each)

  1. Which sorting algorithm repeatedly compares adjacent elements and swaps them if they are in the wrong order? (a) Selection Sort (b) Insertion Sort © Bubble Sort (d) Merge Sort
  2. Which sorting algorithm works by repeatedly finding the minimum ele- ment from the unsorted part and placing it at the beginning? (a) Bubble Sort (b) Selection Sort © Insertion Sort (d) Quick Sort
  3. If you are sorting playing cards in your hand by picking one card at a time and placing it in its correct position among the sorted cards you already hold, which algorithm does this resemble? (a) Selection Sort (b) Bubble Sort © Quick Sort (d) Insertion Sort
  4. Which of these simple sorting algorithms can be efficient if the list is already mostly sorted? (a) Bubble Sort (b) Selection Sort © Insertion Sort (d) All are equally inefficient
  5. What is the primary disadvantage of Bubble Sort, Selection Sort, and Insertion Sort for large datasets? (a) They require extra memory. (b) They cannot sort negative numbers. © They have poor time complexity (typically O(n2)). (d) They only work on numbers, not strings.
  6. If a list is already sorted in ascending order, which algorithm would likely perform the fewest operations? (a) Selection Sort (b) Optimized Bubble Sort (stops if no swaps) © Insertion Sort (d) (b) and © are likely faster than (a)
  7. The process of arranging student records based on their roll numbers in increasing order is an example of: (a) Searching (b) Sorting © Data Struc- ture (d) Abstraction
  8. Merge Sort and Quick Sort are generally preferred over Bub- ble/Selection/Insertion Sort in practice because they are: (a) Easier to implement (b) Require less memory © More efficient (faster time complexity - O(nlogn)) (d) Easier to understand

4.2 Questions from “Finding Needles in Haystacks - Efficient Search- ing” (Based on Section 3.2 Topics)

4.2.1 Subjective Questions (Varying Marks)

  1. Explain the difference in approach between Linear Search and Binary Search. What is the key condition required for Binary Search? (4 Marks)
  2. Trace the steps of Linear Search to find the target 7 in the list [2, 8, 7,

1, 9]. State the index where it is found or if it’s not found. (3 Marks)

  1. Trace the steps of Binary Search to find the target 12 in the sorted list [2, 5, 7, 8, 10, 12, 15]. Show the values of low, high, and mid in each step. (5 Marks)
  2. Why is Binary Search significantly faster than Linear Search for large lists? Explain the concept of eliminating half the search space. (3 Marks)
  3. If sorting a list takes a considerable amount of time, when might it still be beneficial to sort it first before searching? (Relates sorting cost vs. searching benefit). (3 Marks) 4.2.2 Multiple Choice Questions (MCQs) (1 Mark Each)
  4. Which search algorithm checks elements one by one from the beginning? (a) Binary Search (b) Linear Search © Bubble Search (d) Selection Search
  5. What is the main prerequisite for using Binary Search? (a) The list must contain only numbers. (b) The list must have an odd number of elements. © The list must be sorted. (d) The list must not contain duplicate ele- ments.
  6. In the worst-case scenario, how many elements does Linear Search need to check in a list of n elements? (a) 1 (b) n/2 © n (d) logn
  7. In the worst-case scenario, approximately how many comparisons does Binary Search need for a sorted list of n elements? (a) n (b) n/2 © log2n (log base 2 of n) (d) n
  8. You need to find a specific contact name in your phone’s contact list (which is sorted alphabetically). Which search method is most efficient? (a) Linear Search (b) Binary Search © Bubble Search (d) Selection Search
  9. Consider the sorted list [10, 20, 30, 40, 50, 60]. Using Binary Search to find the target 20, what element is checked first? (Assume mid = (low + high) // 2) (a) 10 (b) 20 © 30 (d) 40
  10. Consider the sorted list [10, 20, 30, 40, 50, 60]. Using Binary Search to find the target 55, after checking the middle element (30 or 40), which half of the list will the search continue in? (a) The first half ([10, 20]) (b) The second half ([40, 50, 60] or [50, 60]) © The entire list is checked again. (d) The search stops immediately.
  11. Which search algorithm is generally simpler to implement? (a) Binary Search (b) Linear Search © Merge Search (d) Quick Search
  12. If you need to frequently search for items in a large dataset that is not sorted and cannot be easily sorted, which algorithm must you use? (a) Binary Search (b) Linear Search © Both are equally suitable (d) Neither can be used
  13. The time complexity O(logn) of Binary Search implies that as the list size (n) doubles, the maximum number of comparisons: (a) Doubles (b) Increases by a large amount © Increases only by a small constant amount (e.g., 1 additional comparison) (d) Stays the same 4.3 Application-Specific Questions

(Model idea answers provided in Section 5.3)

  • (3 Marks - Trace): - Start: [6, 2, 4] - Pass 1: Find minimum in [6, 2, 4] → It’s 2. Swap 2 with the first element (6). → State: [2, 6, 4] (Sorted part: [2]) - Pass 2: Find minimum in [6, 4] → It’s 4. Swap 4 with the first element of unsorted part (6). → State: [2, 4, 6] (Sorted part: [2, 4]) - Pass 3: Find minimum in [6] → It’s 6. Swap with itself (no change). → State: [2, 4, 6] (Sorted part: [2, 4, 6])
  • (Guidance: Award marks for clear explanation of finding mini- mum/swapping and accurate trace showing the state after each min- imal element is placed).
  1. Insertion Sort Logic & Trace [6, 2, 4]…(4 Marks)
  • (1 Mark - Logic): Insertion Sort builds the final sorted array one element at a time. It iterates through the input array and for each element, it finds the correct position within the already sorted portion of the array and inserts it there, shifting larger elements one position to the right.
  • (3 Marks - Trace): - Start: [6] is sorted portion. Consider element 2. - Compare 2 with 6. 6>2, shift 6 right →[,6]. Insert 2→ State: [2, 6] (Sorted: [2, 6]) - Consider element 4. - Compare 4 with 6. 6>4, shift 6 right →[2,,6]. Compare 4 with
  1. 2<4, stop shifting. Insert 4→ State: [2, 4, 6] (Sorted: [2, 4, 6])
  • (Guidance: Award marks for clear explanation of inserting into sorted portion/shifting and accurate trace showing the list state after each insertion).
  1. Why Sort First? Example…(3 Marks)
  • (1 Mark - Reason): Sorting arranges data predictably, which allows for the use of much more efficient algorithms for subsequent tasks, especially searching. It also helps in easily finding mini- mum/maximum values, duplicates, or analyzing data distributions.
  • (2 Marks - Example): Searching. Finding a specific item (like a student’s record by roll number) in an unsorted list requires check- ing every single item (Linear Search). In a sorted list, you can use Binary Search, which is dramatically faster as it eliminates half the possibilities with each comparison.
  • (Guidance: Ensure the reason highlights efficiency gains for other tasks and the example clearly benefits from sorted data, like search- ing).
  1. Why Teach Simple Sorts First?..(2 Marks)
  • (2 Marks): These simpler algorithms (Bubble, Selection, Insertion) are taught first primarily because their logic is much easier to un- derstand and visualize for beginners compared to more complex al- gorithms like Merge Sort or Quick Sort. They provide a good con- ceptual foundation for understanding the basic principles of sorting (comparisons, swaps, iterations) before moving on to more optimized, but harder-to-grasp, techniques.
  • (Guidance: Focus should be on pedagogical reasons - simplicity, visualization, building foundational understanding).

5.1.2 MCQ Answers with Explanations:

  1. Answer: © Bubble Sort’s mechanism is based on comparing and poten- tially swapping adjacent elements.
  2. Answer: (b) Selection Sort’s strategy is to select the minimum (or max- imum) from the remaining unsorted part.
  3. Answer: (d) Insertion Sort mimics how many people sort cards – insert- ing each new card into its correct place in the already sorted hand.
  4. Answer: © Insertion Sort performs well on nearly sorted lists because elements don’t need to be shifted far. Optimized Bubble Sort (option b) also performs well if it stops early. Given typical characteristics, Insertion Sort is often cited.
  5. Answer: © Their O(n2) time complexity means the time taken grows quadratically with the input size (n), making them very slow for large n.
  6. Answer: (d) Optimized Bubble Sort would make one pass, find no swaps needed, and stop (O(n)). Insertion Sort would compare each element only with the one before it and do no shifts (O(n)). Selection Sort would still perform all the comparisons (O(n2)).
  7. Answer: (b) Arranging items in a specific order (like increasing roll numbers) is the definition of sorting.
  8. Answer: © Their main advantage is significantly better average-case time complexity (O(nlogn)), making them much faster for large datasets. They are generally harder to understand and implement than the simpler sorts.

5.2 Answers for “Finding Needles in Haystacks - Efficient Searching” Questions

5.2.1 Subjective Question Model Answers & Marking Guidance:

  1. Linear vs. Binary Search…(4 Marks)
    • (1.5 Marks - Linear Search): Linear search checks each element sequentially, starting from the beginning, until the target is found or the end of the list is reached. It does not require the list to be sorted.
    • (1.5 Marks - Binary Search): Binary search works on a sorted list. It compares the target with the middle element. If not found, it eliminates half of the list where the target cannot be and repeats the process on the remaining half.

on that same list afterward. The initial time spent sorting can be offset by the significant time saved during each subsequent search (O(logn) for Binary Search vs. O(n) for Linear Search). If you only need to search once or twice, the sorting cost might not be worth it. But for frequent lookups (common in databases or interactive applications), the upfront sorting cost pays off quickly due to much faster search times.

  • (Guidance: Answer should balance the cost of sorting against the benefit of faster searches, highlighting the importance of the number of subsequent searches ).

5.2.2 MCQ Answers with Explanations:

  1. Answer: (b) Linear Search performs a sequential scan from the start.
  2. Answer: © Binary Search relies fundamentally on the data being sorted to work correctly by dividing the search space.
  3. Answer: © In the worst case (target is the last element or not present), Linear Search checks all n elements.
  4. Answer: © Binary Search eliminates half the search space in each step. The number of steps required is logarithmic (base 2) with respect to n.
  5. Answer: (b) Since the contact list is sorted alphabetically, Binary Search (or a similar efficient method) is feasible and much faster than checking linearly.
  6. Answer: © List: [10, 20, 30, 40, 50, 60]. low=0, high=5. mid=(0+5)//2=2. arr[2] is 30.
  7. Answer: (b) List: [10, 20, 30, 40, 50, 60]. low=0, high=5. mid=2. arr[2] is 30. Target 55>30. Search continues in the right half ([40, 50, 60]). New range: ‘low=mid+1=3, high=5$.
  8. Answer: (b) Linear Search involves a simple loop and comparison, gen- erally considered easier to code than the index manipulation of Binary Search.
  9. Answer: (b) If the list is unsorted, Binary Search cannot be applied. Linear Search is the only option among these if sorting is not feasible.
  10. Answer: © The nature of logarithms means that doubling n only in- creases log2n by 1. (e.g., log21000￿10, log22000￿11). This demonstrates its efficiency for large scale.

5.3 Model Idea Answers & Guidance for Application-Specific Ques- tions:

  1. Finding Top Performers (5 Marks)
    • (2 Marks - Steps):
      1. Sort: Sort the list of student objects based on their exam scores in Descending Order (highest score first).
      2. Select: Take the first 3 student objects from the sorted list.
      3. Extract Names: Get the names from these top 3 student objects.
    • (1 Mark - Data Structure): The students could initially be in a

List or Array of objects/dictionaries.

  • (2 Marks - Why Searching Isn’t Enough): Simple searching (Linear or Binary) is used to find one specific item or check if it exists. To find the Top 3, you need to know the relative ranking of all students’ scores. You could technically find the highest, then find the next highest ignoring the first, etc., but this is inefficient and complicated. Sorting the entire list by score directly arranges them in rank order, making it trivial to pick the top 3 from the beginning.
  • (Guidance: Check for the Sort-then-Select approach and a clear ex- planation of why individual searching is inadequate for finding ranked top elements).
  1. E-commerce Product Filtering (4 Marks)
  • (2 Marks - Sorting Concept): This directly applies the concept of Sorting. The user selects the criteria (price) and the order (ascend- ing for Low to High, descending for High to Low), and the website applies a sorting algorithm to rearrange the displayed product list according to the chosen price order.
  • (2 Marks - Importance for User Experience): This is cru- cial for UX because it allows users to quickly find products within their budget or identify premium/discounted items without manu- ally scanning through potentially hundreds or thousands of products. It makes product discovery much more efficient and less frustrating, leading to a better shopping experience.
  • (Guidance: Ensure sorting is identified as the core concept and the UX benefit focuses on efficiency, user control, and easier decision- making).
  1. Autocomplete Feature (6 Marks)
  • (2 Marks - Linear Search Efficiency): No, Linear Search would be far too slow. Searching through millions of possible words/phrases sequentially for every single letter typed would cause noticeable lag and make the feature unusable (O(N×M) where N is list size, M is prefix length).
  • (2 Marks - Potential Data Structure): Specialized data struc- tures are used. A Trie (Prefix Tree) is commonly used for this. In a Trie, each node represents a character, and paths from the root represent prefixes. Words are stored by traversing paths. (Alterna- tively, a highly optimized sorted list/array combined with modified binary search for prefixes might be mentioned).
  • (2 Marks - Search Optimization): The search needs to be op- timized for prefix matching. When the user types “Jodh”, the system uses the Trie structure to instantly find all words/phrases starting with that prefix by traversing down the corresponding path in the Trie. It doesn’t need to compare “Jodh” against unrelated words like “Apple” or “Mumbai.” The search is constrained to only