Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
+2 votes
594 views
in Computer by (43.0k points)
closed by

NCERT Solutions Class 12, Computer Science, Chapter- 6, Searching

For a deep understanding of this chapter and to excel in Board exams and competitive tests, it's essential to use NCERT Solutions. Created by field experts, these solutions explore all crucial concepts within the chapter, tailored specifically for the CBSE curriculum. They offer thorough support and are an invaluable resource for your academic success.

In these NCERT Solutions for Class 12 Computer Science, we have discussed all types of NCERT intext questions and exercise questions.

Concepts covered in Class 12 Computer Science chapter- 6 Searching, are :

  • Introduction to Searching
  • Linear Search
  • Binary Search
  • Search by Hashing

Our NCERT Solutions for Class 12 Computer Science provide detailed explanations to assist students with their homework and assignments. Proper command and ample practice of topic-related questions provided by our NCERT solutions is the most effective way to achieve full marks in your exams. Begin studying right away to ace your exams.

Get instant access to all solutions and practice questions to jumpstart your preparation right away.

2 Answers

+2 votes
by (43.0k points)
selected by
 
Best answer

NCERT Solutions Class 12, Computer Science, Chapter- 6, Searching

Exercise

1. Using 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.

Solution:

Element 8 is found at position 4 after 4 comparisons.

Element 1 is found at position 1 after 1 comparisons.

Element 99 is not found in the list.

Element 44 is found at position 10.

2. Use 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?

Solution:

Position returned = 4

This means that linear search always returns the first position where the element is present if it is present multiple times in the list.

The linear search function is written as under:

def linearSearch(list, key):

    #function to perform the search

    for index in range(0,len(list)):

        if list[index] == key: #key is present

            return index+1 #position of key in list

    return None #key is not in list

#end of function

3. Write 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.

Solution:

def linearSearch(list, key):
 for index in range(0,len(list)):
  if list[index] == key: 
    return index+1 
 return None 
list1 = [] 
maximum = int(input("How many elements in your list? "))
print("Enter each element and press enter: ") 
for i in range(0,maximum):
 n = int(input())
 list1.append(n) 
print("The List contents are:", list1)
key = int(input("Enter the number to be searched:")) 
position = linearSearch(list1, key)
if position is None:
 print("Number",key,"is not present in the list") 
else:
 print("Number",key,"is present at position",position)

4. Write 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.

Solution:

def binary_search(arr, low, high, x):
    if high >= low:
        mid = (high + low) // 2
        if arr[mid] == x:
            return mid
        elif arr[mid] > x:
            return binary_search(arr, low, mid - 1, x)
        else:
            return binary_search(arr, mid + 1, high, x)
    else:
        return -1
arr = [ 2, 3, 4, 10, 24, 34, 67, 99, 100, 133 ]
x = int(input("Enter the key to be searched: "))
result = binary_search(arr, 0, len(arr)-1, x) 
if result != -1:
    print("Element is present at position", str(result+1))
else:
    print("Element is not present in array")

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

  • 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.

Solution:

According to the Linear Search algorithm the position and number of comparisons for the elements are given as:

1: not found in the array and the number of comparisons is 24.

5: position = 13, number of comparisons is 13.

55: position = 23, number of comparisons are 23.

99: position = 21, number of comparisons are 21.

After sorting,

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]

1: not found in the array and the number of comparisons is 24.

5: position = 1, number of comparisons is 1.

55: position = 12, number of comparisons are 12.

99: position = 24, number of comparisons are 24.

By applying Binary Search:

1: not found in the array

5: position = 1

55: position = 12

99: position = 24

6. Write 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.

Solution:

Linear Search without sorting:
def linearsearch(arr, x):
   for i in range(len(arr)):
      if arr[i] == x:
         return i
   return -1
arr = ["Perfect", "Stupendous", "Wondrous", "Gorgeous", "Awesome", "Mirthful", "Fabulous", "Splendid", "Incredible", "Outstanding", "Propitious", "Remarkable", "Stellar","Unbelievable", "Super", "Amazing"]
x = input("Enter key to be searched: ")
print("element found at index "+str(linearsearch(arr,x)+1))
  • Amazing: Position = 16, number of comparisons = 16

  • Perfect: Position = 1, number of comparisons = 1

  • Great: Position = element not found.

  • Wondrous: Position = 3, number of comparisons = 3

Linear Search after sorting:
def linearsearch(arr, x):
   for i in range(len(arr)):
      if arr[i] == x:
         return i
   return -1
arr = sorted(["Perfect", "Stupendous", "Wondrous", "Gorgeous", "Awesome", "Mirthful", "Fabulous", "Splendid", "Incredible", "Outstanding", "Propitious", "Remarkable", "Stellar","Unbelievable", "Super", "Amazing"])
print("Sorted list: ",arr)
x = input("Enter key to be searched: ")
print("element found at position "+str(linearsearch(arr,x)+1))
  • Amazing: Position = 1, number of comparisons = 1

  • Perfect: Position = 8, number of comparisons = 8

  • Great: Position = element not found.

  • Wondrous: Position = 16, number of comparisons = 16

Binary Search
def binary_search(arr, low, high, x):
    if high >= low:
        mid = (high + low) // 2
        if arr[mid] == x:
            return mid
        elif arr[mid] > x:
            return binary_search(arr, low, mid - 1, x)
        else:
            return binary_search(arr, mid + 1, high, x)
    else:
        return -1
arr =  sorted(["Perfect", "Stupendous", "Wondrous", "Gorgeous", "Awesome", "Mirthful", "Fabulous", "Splendid", "Incredible", "Outstanding", "Propitious", "Remarkable", "Stellar","Unbelievable", "Super", "Amazing"])
print(arr)
x = input("Enter the key to be searched: ")
result = binary_search(arr, 0, len(arr)-1, x)
if result != -1:
    print("Element is present at position", str(result+1))
else:
    print("Element is not present in array")
  • Amazing: Position = 1

  • Perfect: Position = 8

  • Great: Position = element not found.

  • Wondrous: Position = 16

+2 votes
by (43.0k points)

7. Estimate 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?

Solution:

If the record is in the middle of the sorted database, it will take one key comparison for binary search. As linear search is sequential that will take 115 keys comparison. Interpretation:

  • The linear search method is useful for collecting items that are small in size and are not in any particular order.
  • Binary search is a quick searching technique for large datasets which is in ascending or descending order.

8. Use 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.

Solution:

def hashFind(key,hashTable):
 if (hashTable[key % 11] == key): 
  return ((key % 11)) 
 else:
  return None
hashTable=[None, None, None, None, None, None, None, None, None, None]
print("We have created a hashTable of 10 positions:")
print(hashTable)
L = [44, 121, 55,33, 110, 77, 22, 66]
print("The given list is", L[::] )
for i in range(0,len(L)): 
 hashTable[L[i]%10] = L[i]
 print("The hash table contents are: " )
 for i in range(0,len(hashTable)): 
  print("hashindex=", i," , value =", hashTable[i])
key = int(input("Enter the number to be searched:")) 
position = hashFind(key,hashTable)
if the position is None:
 print("Number", key, "is not present in the hash table")
else:
print("Number ",key," present at ",position, " position")

11, and 88 are not present but 44 and 121 are present at index 4 and 1 respectively.

9. Write 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.

Solution:

def hashFind(key, hashTable):
    if (hashTable[len(key) - 1]):
        return print(hashTable[len(key) - 1])
    else:
        return print(None)

CountryCapital = { 'India': 'New Delhi', 'UK': 'London', 'France': 'Paris', 'Switzerland': 'Berne', 'Australia':'Canberra' }
Country = [None, None, None, None, None, None, None, None, None, None, None]
Capital = [None, None, None, None, None, None, None, None, None, None, None]

for k, v in CountryCapital.items():
    length = len(k)
    Country[length - 1] = k
    Capital[length - 1] = v

hashFind('India', Capital)
hashFind('France', Capital)
hashFind('USA', Capital)

Output:
New Delhi
Paris
None

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...