Skip to main content
Chapter 6 of 13
NCERT Solutions

Searching

CBSE · Class 12 · Computer Science

NCERT Solutions for Searching — CBSE Class 12 Computer Science.

43 questions68 flashcards5 concepts

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.

1,000+ Class 12 students started this chapter today

An illustration showing a person searching for an item in a cluttered room, contrasting it with a person finding an item in an organized, labeled storage system, to metaphorically represent the concep
Super Tutor

This is just one of 9+ visuals inside Super Tutor's Searching chapter

Explore the full set
9 Questions Solved · 1 Section

5 worked solutions below. Unlock all 9 free in Super Tutor

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
Use linear search, so compare the key with elements from left to right.

List: [1,2,32,8,17,19,42,13,0,44][1,-2,32,8,17,19,42,13,0,44]

### For key = 8
| Pass | Index | Element | Decision |
|---|---:|---:|---|
| 1 | 0 | 1 | 181 \neq 8 |
| 2 | 1 | -2 | 28-2 \neq 8 |
| 3 | 2 | 32 | 32832 \neq 8 |
| 4 | 3 | 8 | 8=88 = 8, found |

Position found = 4

### For key = 1
| Pass | Index | Element | Decision |
|---|---:|---:|---|
| 1 | 0 | 1 | 1=11 = 1, found |

Position found = 1

### For key = 99
| Pass | Index | Element | Decision |
|---|---:|---:|---|
| 1 | 0 | 1 | 1991 \neq 99 |
| 2 | 1 | -2 | 299-2 \neq 99 |
| 3 | 2 | 32 | 329932 \neq 99 |
| 4 | 3 | 8 | 8998 \neq 99 |
| 5 | 4 | 17 | 179917 \neq 99 |
| 6 | 5 | 19 | 199919 \neq 99 |
| 7 | 6 | 42 | 429942 \neq 99 |
| 8 | 7 | 13 | 139913 \neq 99 |
| 9 | 8 | 0 | 0990 \neq 99 |
| 10 | 9 | 44 | 449944 \neq 99 |

Search is unsuccessful.

### For key = 44
| Pass | Index | Element | Decision |
|---|---:|---:|---|
| 1 | 0 | 1 | 1441 \neq 44 |
| 2 | 1 | -2 | 244-2 \neq 44 |
| 3 | 2 | 32 | 324432 \neq 44 |
| 4 | 3 | 8 | 8448 \neq 44 |
| 5 | 4 | 17 | 174417 \neq 44 |
| 6 | 5 | 19 | 194419 \neq 44 |
| 7 | 6 | 42 | 424442 \neq 44 |
| 8 | 7 | 13 | 134413 \neq 44 |
| 9 | 8 | 0 | 0440 \neq 44 |
| 10 | 9 | 44 | 44=4444 = 44, found |

Position found = 10

Not sure why a step works? check your working in Super Tutor

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
The program returns the position of the first matching element it finds.

For the list [42,2,32,8,17,19,42,13,8,44][42,-2,32,8,17,19,42,13,8,44], the first 88 occurs at index 3, so the program returns position 4.

This means linear search stops as soon as it finds the first occurrence of the key, even if the same value appears again later in the list.

Not sure why a step works? check your working in Super Tutor

3Write a program that takes as input a list having a mix of 10 negative and positive numbers and a key value.Show solution
```python
def linearSearch(lst, key):
for i in range(len(lst)):
if lst[i] == key:
return i + 1
return None

# input list of 10 mixed negative and positive numbers
lst = []
print("Enter 10 integers (negative and positive):")
for _ in range(10):
lst.append(int(input()))

key = int(input("Enter key value: "))
pos = linearSearch(lst, key)

if pos is None:
print("Key is not present in the list")
else:
print("Key is present at position", pos)
```

This program takes 10 integers and a key, searches using linear search, and prints the position if found; otherwise it prints that the key is not present.

Not sure why a step works? check your working in Super Tutor

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
```python
def binarySearch(lst, key):
first = 0
last = len(lst) - 1
while first <= last:
mid = (first + last) // 2
if lst[mid] == key:
return mid + 1
elif key < lst[mid]:
last = mid - 1
else:
first = mid + 1
return None

# input list of 10 integers in ascending order
lst = []
print("Enter 10 integers in ascending order:")
for _ in range(10):
lst.append(int(input()))

key = int(input("Enter key value: "))
pos = binarySearch(lst, key)

if pos is None:
print("Key is not present in the list")
else:
print("Key is present at position", pos)
```

### Example results for 3 keys
For a sorted list such as [3,8,12,15,21,27,34,40,56,72][3, 8, 12, 15, 21, 27, 34, 40, 56, 72]:
- key = 15 → present at position 4
- key = 72 → present at position 10
- key = 19 → not present

Binary search works only on a sorted list.

Not sure why a step works? check your working in Super Tutor

5Following 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]

- 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.
- Use a Python function to sort/arrange the list in ascending order.
- 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.
- 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.
Show solution
Given list:
[50,31,21,28,72,41,73,93,68,43,45,78,5,17,97,71,69,61,88,75,99,44,55,9][50,31,21,28,72,41,73,93,68,43,45,78,5,17,97,71,69,61,88,75,99,44,55,9]

## 1) Linear search on the unsorted list
Linear search compares items from the beginning.

- 1: not present → 24 comparisons
- 5: found at position 1313 comparisons
- 55: found at position 2323 comparisons
- 99: found at position 2121 comparisons

## 2) Sorted list in ascending order
Using sorting, the list becomes:

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

## 3) Linear search again on the sorted list
Linear search still checks from the start, so comparisons depend on where the item appears.

- 1: not present → 24 comparisons
- 5: position 11 comparison
- 55: position 1212 comparisons
- 99: position 2424 comparisons

## 4) Binary search on the sorted list
Binary search uses iterations.

- 1: not present → 5 iterations
- 5: position 15 iterations
- 55: position 121 iteration
- 99: position 245 iterations

## Inference
- In an unsorted list, linear search may need many comparisons.
- In a sorted list, binary search is much faster than linear search.
- Binary search is especially efficient when the key is near the middle.

Not sure why a step works? check your working in Super Tutor

6Write a program that takes as input the following unsorted list of English words:

[Perfect, Stupendous, Wondrous, Gorgeous, Awesome, Mirthful, Fabulous, Splendid, Incredible, Outstanding, Propitious, Remarkable, Stellar, Unbelievable, Super, Amazing].

- Use linear search to find the position of Amazing, Perfect, Great and Wondrous in the list. Also note the number of key comparisons required to find these words in the list.
- Use a Python function to sort the list.
- Again, use linear search to determine the position of Amazing, Perfect, Great and Wondrous in the list and note the number of key comparisons required to find these words in the list.
- Use binary search to determine the position of Amazing, Perfect, Great and Wondrous in the sorted list. Record the number of iterations required in each case.
7Estimate the number of key comparisons required in binary search and linear search if we need to find the details of a person in a sorted database having 230 (1,073,741,824) records when details of the person being searched lies at the middle position in the database. What do you interpret from your findings?
8Use the hash function: h(element) = element%11 to store the collection of numbers: [44, 121, 55, 33, 110, 77, 22, 66] in a hash table. Display the hash table created. Search if the values 11, 44, 88 and 121 are present in the hash table, and display the search results.
9Write a Python program by considering a mapping of list of countries and their capital cities such as:

CountryCapital = {'India': 'New Delhi', 'UK': 'London', 'France': 'Paris', 'Switzerland': 'Berne', 'Australia': 'Canberra'}

Let us presume that our hash function is the length of the Country Name. Take two lists of appropriate size: one for keys (Country) and one for values (Capital). To put an element in the hash table, compute its hash code by counting the number of characters in Country, then put the key and value in both the lists at the corresponding indices. For example, India has a hash code of 5. So, we store India at the 5th position (index 4) in the keys list, and New Delhi at the 5th position (index 4) in the values list and so on. So that we end up with:

| hash index = length of key - 1 | List of Keys | List of Values |
|---|---|---|
| 0 | None | None |
| 1 | UK | London |
| 2 | None | None |
| 3 | Cuba | Havana |
| 4 | India | New Delhi |
| 5 | France | Paris |
| 6 | None | None |
| 7 | None | None |
| 8 | Australia | Canberra |
| 9 | None | None |
| 10 | Switzerland | Berne |

Now search the capital of India, France and the USA in the hash table and display your result.

4 more solved questions in Searching

Every remaining exercise is solved step by step in Super Tutor, plus practice quizzes and flashcards for this chapter. Free to start.

Stuck on a step?

Ask Super Tutor AI to explain any solution on this page in a simpler way — free, 24x7.

Ask a Doubt Free

Frequently Asked Questions

What are the important topics in Searching for CBSE Class 12 Computer Science?
Searching covers several key topics that are frequently asked in CBSE Class 12 board exams. Focus on the core concepts listed on this page and practise related questions to build confidence.
How to score full marks in Searching — CBSE Class 12 Computer Science?
Understand the core concepts first, then work through the 43 practice questions available for this chapter. Revise formulas and definitions regularly, and use flashcards for quick recall before the exam.
Where can I get free NCERT Solutions for Searching Class 12 Computer Science?
This page has free step-by-step NCERT Solutions for every exercise question in Searching (CBSE Class 12 Computer Science) — written the way examiners award marks: given, formula, working, answer.

Sources & Official References

Content is aligned to the official syllabus. Refer to the board website for the latest curriculum.

For serious students

Get the full Searching chapter — for free.

Quizzes, flashcards, AI doubt-solver and a step-by-step study plan for CBSE Class 12 Computer Science.