Skip to main content
Chapter 6 of 13
NCERT Solutions

Searching — NCERT Solutions

CBSE · Class 12 · Computer Science

NCERT question answers for Searching, CBSE Class 12 Computer Science: 9 textbook questions solved step by step. Covers Exercise.

43 questions68 flashcards3 formulas & key relations5 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.

Free trial, no card needed.

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

One of 9 illustrations for Searching in Super Tutor — alongside flashcards, concept maps and practice questions.

See them all
9 Questions Solved · 1 Section

The first 5 solutions are open to read. The other 4 are free with a Super Tutor account. 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

PassIndexElementDecision
1011≠81 \neq 8
21-2−2≠8-2 \neq 8
323232≠832 \neq 8
4388=88 = 8, found

Position found = 4

For key = 1

PassIndexElementDecision
1011=11 = 1, found

Position found = 1

For key = 99

PassIndexElementDecision
1011≠991 \neq 99
21-2−2≠99-2 \neq 99
323232≠9932 \neq 99
4388≠998 \neq 99
541717≠9917 \neq 99
651919≠9919 \neq 99
764242≠9942 \neq 99
871313≠9913 \neq 99
9800≠990 \neq 99
1094444≠9944 \neq 99

Search is unsuccessful.

For key = 44

PassIndexElementDecision
1011≠441 \neq 44
21-2−2≠44-2 \neq 44
323232≠4432 \neq 44
4388≠448 \neq 44
541717≠4417 \neq 44
651919≠4419 \neq 44
764242≠4442 \neq 44
871313≠4413 \neq 44
9800≠440 \neq 44
1094444=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
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
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 13 → 13 comparisons
  • 55: found at position 23 → 23 comparisons
  • 99: found at position 21 → 21 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 1 → 1 comparison
  • 55: position 12 → 12 comparisons
  • 99: position 24 → 24 comparisons

4) Binary search on the sorted list

Binary search uses iterations.

  • 1: not present → 5 iterations
  • 5: position 1 → 5 iterations
  • 55: position 12 → 1 iteration
  • 99: position 24 → 5 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.

Free with a Super Tutor account

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?

Free with a Super Tutor account

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.

Free with a Super Tutor account

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.

Free with a Super Tutor account

4 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 CBSE Class 12 Computer Science?
Key topics in Searching include Core Idea of Searching, Linear Search, Binary Search, Search by Hashing. Study these first, then practise questions on each for the CBSE Class 12 board exam.
Are these NCERT Solutions for Searching free?
The first 5 of the 9 solutions on this page are open to read. The other 4 are free with a Super Tutor account — signing up is free and needs no card.
How should I revise Searching for the CBSE Class 12 board exam?
Learn the core ideas first, then work through the 43 practice questions on Searching. Revise definitions regularly and use flashcards for quick recall before the exam.

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 — start free.

Quizzes, flashcards, an AI doubt solver and a study plan for CBSE Class 12 Computer Science. Free to start, no card needed.