Curriculum
Course: Grade XII Computer Science
Login
Text lesson

Ch-6 Searching

School Revise · Class 11 Computer Science · Chapter 6

Se

 

Code 083, Class 12. S

archingearching finds where a value is in a list. Here we use linear search on any list and binary search on a sorted list.

linear

check one by one

binary

halve each time

What this chapter is about

Searching looks for a value in a list. Linear search checks each item in turn. Binary search is much faster but needs the list to be sorted.

1. Linear search

Go through the list one item at a time until you find the value or reach the end. It works on any list.

nums = [4, 7, 2, 9] target = 2 for i in range(len(nums)): if nums[i] == target: print(“found at”, i) Output: found at 2

2. Binary search

On a sorted list, look at the middle. If the target is smaller, search the left half; if larger, the right half. Each step halves the list, so it is very fast.

3. Which to use

Use linear search for small or unsorted lists. Use binary search for large sorted lists, because it is far quicker.

Practise with the interactive

Explore the idea by tapping. The interactive opens right here in the lesson.

Try it in the code lab

Write and run real Python right here in the lesson, then work through the practice problems with answers.

Starting Python...

Coding practice problems, with answers

Type each one into the code lab above, then open the card to see the worked solution and its output.

Problem 1. Check if a value is in a list.

SOLUTION

nums = [1, 5, 9] print(5 in nums)

OUTPUT

True
Problem 2. Find the index of a value.

SOLUTION

nums = [4, 7, 2, 9] print(nums.index(2))

OUTPUT

2
Problem 3. Linear search with a loop.

SOLUTION

nums = [4, 7, 2, 9] target = 9 for i in range(len(nums)): if nums[i] == target: print(“found at”, i)

OUTPUT

found at 3
Problem 4. Say found or not found.

SOLUTION

nums = [3, 6, 8] target = 5 if target in nums: print(“found”) else: print(“not found”)

OUTPUT

not found
Problem 5. Count how many times a value appears.

SOLUTION

nums = [2, 3, 2, 5, 2] print(nums.count(2))

OUTPUT

3
Problem 6. Find the position of the first even number.

SOLUTION

nums = [3, 7, 4, 9] for i in range(len(nums)): if nums[i] % 2 == 0: print(i) break

OUTPUT

2
Problem 7. Binary search on a sorted list.

SOLUTION

nums = [1, 3, 5, 7, 9] target = 7 lo, hi = 0, len(nums) – 1 while lo <= hi: mid = (lo + hi) // 2 if nums[mid] == target: print(“found at”, mid) break elif nums[mid] < target: lo = mid + 1 else: hi = mid – 1

OUTPUT

found at 3
Problem 8. Find the largest value.

SOLUTION

print(max([3, 9, 5]))

OUTPUT

9
Problem 9. Find the smallest value.

SOLUTION

print(min([3, 9, 5]))

OUTPUT

3
Problem 10. Write a linear search function.

SOLUTION

def search(nums, target): for i in range(len(nums)): if nums[i] == target: return i return -1 print(search([4, 7, 2], 7))

OUTPUT

1

Practice set A, multiple choice

1. Which search checks each item one by one?

Linear search.

2. Which search needs a sorted list?

Binary search.

3. Binary search looks first at the …

middle item.

4. Which search is faster for a large sorted list?

Binary search.

Quick summary

Searching finds a value in a list. Linear search checks items one by one and works on any list. Binary search needs a sorted list and looks at the middle, then halves the search each step, so it is much faster. Use linear for small or unsorted lists and binary for large sorted lists.

Open the Virtual Lab

These free Class 12 Computer Science notes explain searching, linear search on any list, binary search on a sorted list, and when to use each with worked examples and practice, for CBSE students across India and the Gulf including the UAE, Saudi Arabia, Qatar, Oman, Kuwait and Bahrain.

© 2026 School Revise. All rights reserved. Original content aligned to the CBSE and NCERT Class 12 Computer Science syllabus. Unauthorised copying is not permitted.

Layer 1
Login Categories