Computer Science · Class 12 Optional
Ch 6Searching — Class 12 Computer Science, concept-first.
We store things at home and later look for them when needed. Sometimes we remember exactly where an item is kept; other times we do not, and we have to search for it. A computer works in a similar way — it stores large amounts of data and must retrieve that data later when a user or a program asks for it.
Key concepts
Hover a concept to preview it and jump to its most relevant Q&A.
Linear Search
Imagine you have a stack of exam answer sheets on your desk, and you need to find the one belonging to a student named "Priya." You don't have them sorted alphabetically or by roll number.
Most relevant Q&A
- Assume that the numList has seven elements [8, -4, 7, 17, 0, 2, 19] so, n = 7. We need to search for the key, say 17 in numList. Table 6.1 s…Preview
- Consider a sorted list comprising of 15 elements: numList = [2,3,5,7,10,11,12,17,19,23,29,31,37,41,43] We need to search for the key, say 17…Preview
- 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 showi…Free
- 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,…Free
- 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 whe…Free
Chapter contents
The NCERT structure, section by section. Open a section to see its questions, then read the concept-first solution.
Introduction
We store things at home and later look for them when needed. Sometimes we remember exactly where an item is kept; other times we do not, and we have to search for it.
Linear Search
3 QLinear search is the simplest way to find an item in a list. It works by checking every single element, one after another, from the very first element to the last.
+−Worked Examples1 question
+−Activities1 question
Binary Search
4 QImagine you need to find the meaning of the word "Zoology" in an English dictionary. Would you start from the first page and flip through every single page until you reach Z? Of course not.
+−Worked Examples1 question
+−Activities1 question
+−Activities1 question
Applications of Binary Search
Binary search is not just a technique for locating one value in a list — the same idea shows up, in one form or another, wherever fast lookup matters:
Search by Hashing
Hashing is a search technique designed to answer one question in a single step: Is this key present in the list? The logic is simple: if you already know exactly which index position every value shoul…
Collision
Hashing works beautifully when every element in a list maps to a unique slot in the hash table. But that ideal situation does not always happen. Consider a list like [34, 16, 2, 26, 80].
Summary
- Searching: the process of trying to locate a particular element, called the key, within a collection of elements.
Exercises
+−Show 9 questionsHide questions9 questions
- Q1Using 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 showi…Free
- Q2Use 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,…Free
- Q3Write 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 whe…Free
- Q4Write 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 th…Preview
- Q5Following 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, 5…Preview
- Q6Write a program that takes as input the following unsorted list of English words: [Perfect, Stupendous, Wondrous, Gorgeous, Awesome, Mirthfu…Preview
- Q7Estimate 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 da…Preview
- Q8Use 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. Displa…Preview
- Q9Write a Python program by considering a mapping of list of countries and their capital cities such as: CountryCapital= {'India':'New Delhi',…Preview