Searching — NCERT Solutions
Karnataka Board · Class 12 · Computer Science
NCERT Solutions for Searching, Karnataka Board Class 12 Computer Science: 11 textbook questions solved step by step.
Interactive on Super Tutor
Studying Searching? Get the full interactive chapter.
Quizzes, flashcards, AI doubt-solver and a step-by-step study plan — built for NCERT solutions and more.
Free trial, no card needed.

One of 9 illustrations for Searching in Super Tutor — alongside flashcards, concept maps and practice questions.
The first 6 solutions are open to read. The other 5 are free with a Super Tutor account.
Activity 6.3
6.3Consider the numList [17, 8, -4, 7, 0, 2, 19]. Sort it using the sort() function of Python's Lists. Now apply binary search to search for the key 7. Determine the number of iterations required.Show solution
Step 1 – Sort the list using sort():
Original list:
After sorting:
Indices:
Step 2 – Apply Binary Search for key = 7:
Given: , ,
Iteration 1:
Key found at index 3 (Position 4).
Number of iterations required = 1
Since 7 happens to be the middle element of the sorted list, binary search finds it in just 1 iteration.
Activity 6.4
6.4Consider a list [-4, 0, 2, 7, 8, 17, 19]. Apply binary search to find element -4. Determine the number of key comparisons required.Show solution
Given: , ,
Initially: ,
Iteration 1:
Comparison 1: ? → No
Comparison 2: ? → Yes →
Iteration 2:
Comparison 3: ? → No
Comparison 4: ? → Yes →
Iteration 3:
Comparison 5: ? → Yes → Key found at index 0 (Position 1).
Total number of key comparisons required = 5 (across 3 iterations).
Note: In each iteration two comparisons are made (equality check and greater-than check) except the last iteration where the key is found on the equality check itself, giving comparisons.
Exercise
1Using linear search determine the position of 8, 1, 99 and 44 in the list: [1, -2, 32, 8, 17, 19, 42, 13, 0, 44]. Draw a detailed table showing the values of the variables and the decisions taken in each pass of linear search.Show solution
Concept: In linear search, each element of the list is compared with the key one by one from the beginning. The position (1-based) is returned when the key is found; otherwise 'Not Found' is reported.
Given list: (indices 0–9)
Search for key = 8:
| Pass | Index (i) | numList[i] | numList[i] == 8? | Decision |
|---|---|---|---|---|
| 1 | 0 | 1 | No | Continue |
| 2 | 1 | -2 | No | Continue |
| 3 | 2 | 32 | No | Continue |
| 4 | 3 | 8 | Yes | Found at position 4 |
Result: 8 is found at position 4.
Search for key = 1:
| Pass | Index (i) | numList[i] | numList[i] == 1? | Decision |
|---|---|---|---|---|
| 1 | 0 | 1 | Yes | Found at position 1 |
Result: 1 is found at position 1.
Search for key = 99:
| Pass | Index (i) | numList[i] | numList[i] == 99? | Decision |
|---|---|---|---|---|
| 1 | 0 | 1 | No | Continue |
| 2 | 1 | -2 | No | Continue |
| 3 | 2 | 32 | No | Continue |
| 4 | 3 | 8 | No | Continue |
| 5 | 4 | 17 | No | Continue |
| 6 | 5 | 19 | No | Continue |
| 7 | 6 | 42 | No | Continue |
| 8 | 7 | 13 | No | Continue |
| 9 | 8 | 0 | No | Continue |
| 10 | 9 | 44 | No | Continue |
| — | End | — | — | Not Found |
Result: 99 is not present in the list.
Search for key = 44:
| Pass | Index (i) | numList[i] | numList[i] == 44? | Decision |
|---|---|---|---|---|
| 1 | 0 | 1 | No | Continue |
| 2 | 1 | -2 | No | Continue |
| 3 | 2 | 32 | No | Continue |
| 4 | 3 | 8 | No | Continue |
| 5 | 4 | 17 | No | Continue |
| 6 | 5 | 19 | No | Continue |
| 7 | 6 | 42 | No | Continue |
| 8 | 7 | 13 | No | Continue |
| 9 | 8 | 0 | No | Continue |
| 10 | 9 | 44 | Yes | Found at position 10 |
Result: 44 is found at position 10.
Python Program:
def linearSearch(numList, key):
for i in range(len(numList)):
if numList[i] == key:
return i + 1 # 1-based position
return -1
numList = [1, -2, 32, 8, 17, 19, 42, 13, 0, 44]
for key in [8, 1, 99, 44]:
pos = linearSearch(numList, key)
if pos != -1:
print(f"Key {key} found at position {pos}")
else:
print(f"Key {key} not found in the list")2Use the linear search program to search the key with value 8 in the list having duplicate values such as [42, -2, 32, 8, 17, 19, 42, 13, 8, 44]. What is the position returned? What does this mean?Show solution
Given list: ,
Applying Linear Search:
| Pass | Index (i) | numList[i] | numList[i] == 8? | Decision |
|---|---|---|---|---|
| 1 | 0 | 42 | No | Continue |
| 2 | 1 | -2 | No | Continue |
| 3 | 2 | 32 | No | Continue |
| 4 | 3 | 8 | Yes | Found at position 4 |
Position returned = 4
Interpretation:
Linear search returns the position of the first occurrence of the key in the list. Even though 8 appears again at index 8 (position 9), the search stops as soon as the first match is found at index 3 (position 4). This means that when duplicate values are present in a list, linear search will always return the position of the first occurrence of the key and will not search further.
Python Program:
def linearSearch(numList, key):
for i in range(len(numList)):
if numList[i] == key:
return i + 1
return -1
numList = [42, -2, 32, 8, 17, 19, 42, 13, 8, 44]
key = 8
pos = linearSearch(numList, key)
if pos != -1:
print(f"Key {key} found at position {pos}")
else:
print(f"Key {key} not found")Output: Key 8 found at position 4
3Write a program that takes as input a list having a mix of 10 negative and positive numbers and a key value. Apply linear search to find whether the key is present in the list or not. If the key is present it should display the position of the key in the list otherwise it should print an appropriate message. Run the program for at least 3 different keys and note the result.Show solution
Concept: Linear search iterates through each element of the list and compares it with the key. If a match is found, the position is displayed; otherwise, a 'not found' message is printed.
Python Program:
def linearSearch(numList, key):
"""Returns 1-based position of key in numList, or -1 if not found."""
for i in range(len(numList)):
if numList[i] == key:
return i + 1
return -1
# Input: a list with a mix of negative and positive numbers
numList = [-15, 3, -7, 42, 18, -2, 9, -33, 27, 5]
print("List:", numList)
# Taking key as input from user
key = int(input("Enter the key to search: "))
pos = linearSearch(numList, key)
if pos != -1:
print(f"Key {key} found at position {pos} in the list.")
else:
print(f"Key {key} is not present in the list.")Sample Runs:
Run 1: key = 42
Run 2: key = -33
Run 3: key = 100
Observation: Linear search correctly identifies the position of a key if present, and reports 'not found' otherwise. It works on both sorted and unsorted lists.
4Write a program that takes as input a list of 10 integers and a key value and applies binary search to find whether the key is present in the list or not. If the key is present it should display the position of the key in the list otherwise it should print an appropriate message. Run the program for at least 3 different key values and note the results.Show solution
Concept: Binary search works on a sorted list. It repeatedly divides the search interval in half. If the key equals the middle element, the search is successful. If the key is less than the middle element, search continues in the left half; otherwise in the right half.
Python Program:
def binarySearch(numList, key):
"""Returns 1-based position of key, or -1 if not found."""
first = 0
last = len(numList) - 1
while first <= last:
mid = (first + last) // 2
if numList[mid] == key:
return mid + 1 # 1-based position
elif numList[mid] > key:
last = mid - 1
else:
first = mid + 1
return -1
# Input list of 10 integers
numList = [3, 7, 15, 22, 34, 45, 58, 67, 79, 91]
numList.sort() # Binary search requires sorted list
print("Sorted List:", numList)
# Taking key as input
key = int(input("Enter the key to search: "))
pos = binarySearch(numList, key)
if pos != -1:
print(f"Key {key} found at position {pos} in the list.")
else:
print(f"Key {key} is not present in the list.")Sample Runs:
Run 1: key = 34
- ; → Found at position 5
Run 2: key = 7
- Iteration 1: , →
- Iteration 2: , → Found at position 2
Run 3: key = 50
- Iteration 1: , →
- Iteration 2: , →
- Iteration 3: , →
- Iteration 4: , →
- → Not found
Observation: Binary search is significantly faster than linear search for large sorted lists.
Free with a Super Tutor account
Free with a Super Tutor account
Free with a Super Tutor account
Free with a Super Tutor account
Free with a Super Tutor account
5 more solved questions in Searching
They are free with a Super Tutor account, along with practice quizzes and flashcards for this chapter. Free to start, no card needed.
Frequently Asked Questions
What are the important topics in Searching for Karnataka Board Class 12 Computer Science?
Are these NCERT Solutions for Searching free?
How should I revise Searching for the Karnataka Board Class 12 board exam?
Sources & Official References
- Karnataka SSLC — kseeb.kar.nic.in
- Dept of Pre-University Education, Karnataka
- National Education Policy 2020 — education.gov.in
Content is aligned to the official syllabus. Refer to the board website for the latest curriculum.
More resources for Searching
Practice Quiz
Test yourself with a quick quiz
Important Questions
Exam-style questions with answers
Revision Notes
Key points for last-minute revision
Formula Sheet
The chapter's formulas in one place
Chapter Summary
Understand the chapter at a glance
Concept Maps
See how topics connect
Study Plan
Step-by-step plan for this chapter
Flashcards
Quick-fire cards for active recall
Syllabus
What topics to cover
For serious students
Get the full Searching chapter — start free.
Quizzes, flashcards, an AI doubt solver and a study plan for Karnataka Board Class 12 Computer Science. Free to start, no card needed.