Monday, 19 October 2026Mon, 19 Oct 2026 All-India deskIndia Admit cardsAdmit Results
Quizzes Am I eligible?

Class 12 Computer Science Ch 6: Searching Summary & Notes

Updated for NCERT 2026-27 syllabus 📅 10 August 2026
WhatsApp X
Class 12 Computer Science Ch 6: Searching Summary & Notes, NCERT Summaries · Class 12 | jijnas.com

Chapter Overview

This chapter introduces fundamental techniques for locating specific elements within data collections. You will learn about the concept of searching, why it's important, and how to measure the efficiency of different search algorithms using key comparisons. The chapter focuses on three primary methods: Linear Search, Binary Search, and Search by Hashing, detailing their working principles, advantages, disadvantages, and practical applications, preparing you for both theoretical understanding and programming implementation.

Easy Explanation

Imagine you have a stack of books and you need to find a specific one. How would you do it? You'd probably use a 'searching' method!

Searching in Computer Science is all about finding a particular item (called the 'key') within a collection of items, like a list or an array. We want to know if the item is there and, if so, where it is located. The efficiency of a search is often measured by how many 'key comparisons' it takes, basically, how many times you have to check your target item against an item in the collection.

Think of searching like finding a friend in a crowd. If you just walk through the crowd checking each person one by one, that's like a Linear Search. If the crowd is perfectly lined up by height and you know your friend's height, you could quickly narrow down where they might be, which is more like a Binary Search. If everyone has a unique badge number that tells you exactly where they are supposed to stand, that's similar to Hashing.

We'll explore three main ways to search:

1. Linear Search: This is the simplest. You start from the beginning of your list and check each item one by one until you find what you're looking for or reach the end. It works on any list, sorted or unsorted, but can be slow for large lists.
2. Binary Search: This is much faster, but it has a crucial requirement: your list MUST be sorted. It works by repeatedly dividing the list in half. You check the middle item. If it's your target, great! If your target is smaller, you ignore the second half; if larger, you ignore the first half. You keep halving the remaining list until you find your item or the list becomes empty.
3. Search by Hashing: This is super-fast in many cases. It uses a special function, called a 'hash function', to directly calculate where an item should be stored in a special table (a 'hash table'). When you want to find an item, you just run its value through the hash function, and it tells you exactly where to look. Sometimes, different items might want to go to the same spot (a 'collision'), which needs to be handled, but generally, it's very quick.

Important Concepts

  • Searching: The process of locating a specific element (the 'key') within a collection of data and determining its presence and position.
  • Key Comparison: The fundamental operation used to measure the work done by a search algorithm; it counts each time a stored value is checked against the search key.
  • Linear Search (Sequential Search):
    • Simplest search algorithm.
    • Checks each element sequentially from the beginning until the key is found or the list ends.
    • Works on both sorted and unsorted lists.
    • Worst-case time complexity: O(n) (key is last or not present).
    • Best-case time complexity: O(1) (key is the first element).
    • Suitable for small or unsorted datasets.
  • Binary Search:
    • An efficient search algorithm that requires the list to be sorted.
    • Works by repeatedly dividing the search interval in half.
    • Compares the key with the middle element. If equal, found. If key is smaller, searches the left half; if larger, searches the right half.
    • Worst-case time complexity: O(log n).
    • Best-case time complexity: O(1) (key is the middle element).
    • Significantly faster than linear search for large sorted lists.
  • Search by Hashing:
    • Uses a hash function to compute an index (address) directly for an element.
    • Elements are stored in a hash table.
    • Aims for O(1) (constant time) search in the best case (no collisions).
    • Collision: Occurs when two different keys produce the same hash value.
    • A simple hash function for numbers: `h(element) = element % size`.
    • Very efficient for data retrieval if collisions are handled well.

Key Terms

Key
The specific element or value that is being searched for within a collection.
Key Comparison
The operation of comparing the search key with an element from the data collection, used to measure algorithm efficiency.
Linear Search
A sequential search algorithm that checks each element in a list one by one until the key is found or the list ends.
Binary Search
An efficient search algorithm that works on sorted lists by repeatedly dividing the search interval in half.
Hash Function
A function that takes an input (key) and returns an index or location where the key should be stored or found in a hash table.
Hash Table
A data structure that implements an associative array abstract data type, mapping keys to values using a hash function.
Collision
In hashing, a situation where two different keys produce the same hash value (index).
Time Complexity
A measure of the amount of time taken by an algorithm to run as a function of the length of the input (e.g., O(n), O(log n), O(1)).

Diagram/Visual Explanation

1. Linear Search Process

Imagine a list of numbers: `[15, 20, 8, 35, 10]` and you want to find `8`.

* Step 1: Start at the first element (index 0). Is `15 == 8`? No.
* Step 2: Move to the next element (index 1). Is `20 == 8`? No.
* Step 3: Move to the next element (index 2). Is `8 == 8`? Yes! Found at index 2.

If the key was `50`, you would continue checking until the end of the list and conclude it's not present.

2. Binary Search Process

Imagine a sorted list of numbers: `[10, 20, 30, 40, 50, 60, 70]` and you want to find `60`.

* Step 1: Identify `low = 0`, `high = 6`. Calculate `mid = (0 + 6) // 2 = 3`. Element at `mid` is `40`.
* Is `60 == 40`? No.
* Is `60 < 40`? No. So, `60` must be in the right half.
* Step 2: Update `low = mid + 1 = 4`. `high` remains `6`. New search range: `[50, 60, 70]`.
* Calculate `mid = (4 + 6) // 2 = 5`. Element at `mid` is `60`.
* Is `60 == 60`? Yes! Found at index 5.

3. Hashing Process (Simple Remainder Method)

Imagine a hash table of `size = 5` and you want to store `[15, 22, 18, 30]`.

* Hash Function: `h(element) = element % 5`

* Insert 15: `h(15) = 15 % 5 = 0`. Store `15` at index `0`.
`[15, _, _, _, _]`

* Insert 22: `h(22) = 22 % 5 = 2`. Store `22` at index `2`.
`[15, _, 22, _, _]`

* Insert 18: `h(18) = 18 % 5 = 3`. Store `18` at index `3`.
`[15, _, 22, 18, _]`

* Insert 30: `h(30) = 30 % 5 = 0`. Collision! Index `0` is already occupied by `15`. (Collision resolution techniques would be applied here, e.g., finding the next available slot).

* Search for 18: `h(18) = 18 % 5 = 3`. Go directly to index `3`. Is element at index `3` equal to `18`? Yes! Found.

What You Must Remember

  • Linear Search works on any list (sorted/unsorted) with O(n) worst-case time complexity, while Binary Search requires a sorted list and offers O(log n) efficiency.
  • Binary Search's best case is O(1) when the target is the middle element, not necessarily the first.
  • Hashing aims for O(1) search time by directly computing an index using a hash function, but collisions can degrade performance.
  • Key comparisons are crucial for analyzing algorithm efficiency; dry-run questions often test this.
  • For board exams, understand the algorithms, their pre-requisites, time complexities, and be able to trace their execution (dry-run) and write basic Python code.

Quick Revision

Searching finds a 'key' in a collection. Linear Search checks elements one-by-one (O(n), works on any list). Binary Search efficiently halves a sorted list (O(log n), best case O(1) if middle element). Hashing uses a hash function to compute a direct index (O(1) best case), storing elements in a hash table; collisions occur when different keys map to the same index.

Linear Search Worst Case: O(n) comparisons
Binary Search Worst Case: O(log₂n) comparisons
Hashing Best Case: O(1) comparisons (no collision)

Important Questions

1. Define searching and state its primary goal. (1 Mark)
* Answer Points: Locating a specific element (key) within a collection; determining presence and position.

2. Differentiate between Linear Search and Binary Search based on their prerequisites and time complexity. (2 Marks)
* Answer Points: Linear: No prerequisite, O(n) worst case. Binary: List must be sorted, O(log n) worst case.

3. Explain the working principle of Binary Search with a suitable example. (3 Marks)
* Answer Points: Requires sorted list. Repeatedly divides list in half, compares key with middle element, narrows search to left/right half. Example: `[10, 20, 30, 40, 50]`, search `40`.

4. Consider the list `[25, 12, 38, 7, 45, 19]`. How many comparisons would a Linear Search make to find the element `45`? What if `50` is searched? (2 Marks)
* Answer Points: For `45`: 5 comparisons. For `50`: 6 comparisons (worst case, not found).

5. What is a hash function? Explain with a simple example how it helps in searching. (3 Marks)
* Answer Points: Function to compute an index for an element. Example: `h(key) = key % size`. Direct access to element's location in hash table, aiming for O(1) search.

6. Write a Python function for Linear Search that takes a list and a key as input and returns the index of the key if found, otherwise -1. (5 Marks)
* Answer Points: Loop through list using `range(len(list))`. If `list[i] == key`, return `i`. If loop finishes, return `-1`.

7. Discuss the advantages and disadvantages of Linear Search. When is it preferred over other search methods? (3 Marks)
* Answer Points: Advantages: Simple, works on unsorted lists. Disadvantages: Slow for large lists. Preferred: Small datasets, unsorted data, infrequent searches.

8. What is a 'collision' in the context of hashing? Briefly explain why it's a challenge. (2 Marks)
* Answer Points: When two different keys map to the same hash table index. Challenge: Requires extra steps (collision resolution) to find an alternative storage/retrieval location, degrading O(1) performance.

9. Given a sorted list `[5, 10, 15, 20, 25, 30, 35]`, trace the steps of Binary Search to find the element `15`. Count the number of key comparisons made. (3 Marks)
* Answer Points:
1. `low=0, high=6, mid=3`. `list[3]=20`. `15 10`. (1 comparison)
3. `low=2`. `low=2, high=2, mid=2`. `list[2]=15`. `15 == 15`. Found. (1 comparison)
* Total comparisons: 3.

10. Explain two real-world applications where searching algorithms are extensively used. (2 Marks)
* Answer Points: Phone contact apps (searching names), online retail apps (searching products), library management systems (searching books), website search bars.

🧭
What else are you preparing for?Most people run two or three exam tracks at once. Tap your other one, check your age, qualification and attempts in 10 seconds.
Every eligibility check, one page →

Discussion

0 be kind · most people here are students
No comments yet, be the first voice here. 🌱