Skip to content
Exercises · Q5

Q.Following is a list of unsorted/unordered numbers:
[50, 31, 21, 28, 72, 41, 73, 93, 68, 43, 45, 78, 5, 17, 97, 71, 69, 61, 88, 75, 99, 44, 55,9]

(a) Use linear search to determine the position of 1, 5, 55 and 99 in the list. Also note the number of key comparisons required to find each of these numbers in the list.
(b) Use a Python function to sort/arrange the list in ascending order.
(c) Again, use linear search to determine the position of 1, 5, 55 and 99 in the list and note the number of key comparisons required to find these numbers in the list.
(d) Use binary search to determine the position of 1, 5, 55 and 99 in the sorted list. Record the number of iterations required in each case.
Tripura TbseTextbookSubjective· 3mImportance★★★★★
71% · 12/17 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

This question tests your understanding of linear search (unsorted vs sorted) and binary search (sorted only), comparing their efficiency through actual key comparisons and iterations.

The core idea here is simple: searching is about finding an element's position, and sorting makes certain searches dramatically faster. Linear search checks every element one by one — it doesn't care if the list is sorted or not. Binary search, on the other hand, only works on a sorted list and cuts the search space in half at every step.

Let's work through each part systematically.


(a) Linear Search on the Unsorted List

The given list is:

[50, 31, 21, 28, 72, 41, 73, 93, 68, 43, 45, 78, 5, 17, 97, 71, 69, 61, 88, 75, 99, 44, 55, 9]

Linear search works by starting at index 0 and moving forward until we find the target. The number of key comparisons equals the position (1-based) where the element is found — because we compare each element until we hit the target.

Let's trace each target:

For 1: The list has no element equal to 1. We compare all 24 elements and never find it. So position = "not found", comparisons = 24.

For 5: Starting from index 0: 50, 31, 21, 28, 72, 41, 73, 93, 68, 43, 45, 78, then 5 at index 12 (0-based). That's 13 comparisons (1-based position 13).

For 55: We scan: 50, 31, 21, 28, 72, 41, 73, 93, 68, 43, 45, 78, 5, 17, 97, 71, 69, 61, 88, 75, 99, 44, then 55 at index 22. That's 23 comparisons.

For 99: Scan: 50, 31, 21, 28, 72, 41, 73, 93, 68, 43, 45, 78, 5, 17, 97, 71, 69, 61, 88, 75, then 99 at index 20. That's 21 comparisons.

Watch out

Remember: position in the question likely means 1-based index (the element's place in the list, starting from 1). The 0-based index is one less. Always check what the question expects — here, "position" means 1-based.


(b) Python Function to Sort in Ascending Order

We'll write a clean function. Any sorting algorithm works — let's use Python's built-in sort() for reliability, but I'll also show a manual implementation to demonstrate understanding.

def sort_list(arr):
    # Using Python's built-in Timsort — efficient and correct
    arr.sort()
    return arr

# Alternatively, a manual bubble sort for teaching:
def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
    return arr

# Given list
numbers = [50, 31, 21, 28, 72, 41, 73, 93, 68, 43, 45, 78, 5, 17, 97, 71, 69, 61, 88, 75, 99, 44, 55, 9]
sorted_numbers = sort_list(numbers.copy())  # copy to preserve original
print("Sorted list:", sorted_numbers)

Expected output:

Sorted list: [5, 9, 17, 21, 28, 31, 41, 43, 44, 45, 50, 55, 61, 68, 69, 71, 72, 73, 75, 78, 88, 93, 97, 99]
Tip

Using arr.copy() is a good habit — it prevents the original list from being modified. In exams, always show that you understand the difference between mutating and returning.


(c) Linear Search on the Sorted List

Now the sorted list is:

[5, 9, 17, 21, 28, 31, 41, 43, 44, 45, 50, 55, 61, 68, 69, 71, 72, 73, 75, 78, 88, 93, 97, 99]

Linear search still checks from the start. Let's trace:

For 1: Not present. Compare all 24 elements. Comparisons = 24.

For 5: First element is 5. Found at position 1 (1-based). Comparisons = 1.

For 55: Scan: 5, 9, 17, 21, 28, 31, 41, 43, 44, 45, 50, then 55 at position 12. Comparisons = 12.

For 99: Scan all the way to the end: 5, 9, 17, 21, 28, 31, 41, 43, 44, 45, 50, 55, 61, 68, 69, 71, 72, 73, 75, 78, 88, 93, 97, then 99 at position 24. Comparisons = 24.

Note

| Target | Position (1-based) | Comparisons |

|--------|-------------------|-------------|

| 1 | Not found | 24 |

| 5 | 1 | 1 |

| 55 | 12 | 12 |

| 99 | 24 | 24 |

Notice that sorting helped 5 (found immediately) and 55 (found earlier than before), but 99 actually took more comparisons because it moved to the end. Linear search doesn't benefit from sorting in the worst case.


(d) Binary Search on the Sorted List

Binary search works by repeatedly dividing the search interval in half. We maintain low and high indices, compute mid, and compare. Each division is one iteration.

Let's trace each target on the sorted list (0-based indices 0 to 23):

For 1 (not present):

  • Iteration 1: low=0, high=23, mid=11 → value 55 > 1, so high=10
  • Iteration 2: low=0, high=10, mid=5 → value 31 > 1, so high=4
  • Iteration 3: low=0, high=4, mid=2 → value 17 > 1, so high=1
  • Iteration 4: low=0, high=1, mid=0 → value 5 > 1, so high=-1
  • low > high, stop. Not found. Iterations = 4.

For 5:

  • Iteration 1: low=0, high=23, mid=11 → 55 > 5, high=10
  • Iteration 2: low=0, high=10, mid=5 → 31 > 5, high=4
  • Iteration 3: low=0, high=4, mid=2 → 17 > 5, high=1
  • Iteration 4: low=0, high=1, mid=0 → 5 == 5, found at index 0. Iterations = 4.

For 55:

  • Iteration 1: low=0, high=23, mid=11 → 55 == 55, found at index 11. Iterations = 1.

For 99:

  • Iteration 1: low=0, high=23, mid=11 → 55 < 99, low=12 …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.