Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🧱 Unit 1 – Data Structures और Algorithms  ·  Lesson 13

Searching: Linear, Binary, Hashing

Searching क्या है?

Searching मतलब पता करना कि कोई value (key) collection में है या नहीं, और है तो कहाँ। हम तीन तरीके पढ़ेंगे: linear search, binary search और hashing।

Linear search

शुरू से हर element एक-एक करके check करें जब तक key न मिले या list ख़त्म न हो। यह unsorted data पर भी चलता है।

17 खोजें →80-4172173found0425196
Index 3 तक पहुँचने में 4 comparisons
Python
def linear_search(lst, key):
    for i in range(len(lst)):
        if lst[i] == key:
            return i               # position found
    return -1                      # not found

data = [8, -4, 7, 17, 0, 2, 19]
print('17 found at index', linear_search(data, 17))
print('5 found at index', linear_search(data, 5))
17 found at index 3 5 found at index -1

Binary search

सिर्फ़ sorted list पर चलता है। बीच वाला element देखें: वही key है तो रुकें; key बड़ी है तो दाएँ आधे में, नहीं तो बाएँ आधे में आगे खोजें। हर step में खोजने की जगह आधी हो जाती है।

  1. 1low = 0, high = n-1
  2. 2mid = (low + high) // 2
  3. 3key == list[mid] → मिल गया
  4. 4key > list[mid] → low = mid + 1
  5. 5key < list[mid] → high = mid - 1
  6. 6low <= high तक दोहराएँ; नहीं तो not found
Binary search algorithm
Python
def binary_search(lst, key):
    low, high = 0, len(lst) - 1
    step = 0
    while low <= high:
        mid = (low + high) // 2
        step += 1
        print('Step', step, ': low', low, 'high', high, 'mid', mid, '->', lst[mid])
        if lst[mid] == key:
            return mid
        elif key > lst[mid]:
            low = mid + 1          # search right half
        else:
            high = mid - 1         # search left half
    return -1

nums = [2, 3, 5, 7, 10, 11, 12, 17, 19, 23, 29, 31, 37, 41, 43]
print('Found at', binary_search(nums, 17))
print('Found at', binary_search(nums, 4))
Step 1 : low 0 high 14 mid 7 -> 17 Found at 7 Step 1 : low 0 high 14 mid 7 -> 17 Step 2 : low 0 high 6 mid 3 -> 7 Step 3 : low 0 high 2 mid 1 -> 3 Step 4 : low 2 high 2 mid 2 -> 5 Found at -1

Comparisons: linear vs binary

Python
def count_linear(lst, key):
    c = 0
    for x in lst:
        c += 1
        if x == key:
            break
    return c
def count_binary(lst, key):
    low, high, c = 0, len(lst) - 1, 0
    while low <= high:
        mid = (low + high) // 2; c += 1
        if lst[mid] == key: break
        if key > lst[mid]: low = mid + 1
        else: high = mid - 1
    return c
big = list(range(1, 1001))          # 1..1000 sorted
for k in [1, 500, 1000]:
    print('key', k, '| linear:', count_linear(big, k), '| binary:', count_binary(big, k))
key 1 | linear: 1 | binary: 9 key 500 | linear: 500 | binary: 1 key 1000 | linear: 1000 | binary: 10
Worst-case comparisons050100150comparisonsn=8n=16n=32n=64n=128Linear (worst)Binary (worst)
Binary search को बहुत कम comparisons चाहिए

Hashing

Hashing हर key को hash function से निकली position पर रखता है, जैसे key % size। Search के लिए वही position निकालकर एक बार देखते हैं – scan की ज़रूरत नहीं।

Hash table (size 10) →800–122933344–5166777–8–9
Position = key % 10
Python
SIZE = 10
table = [None] * SIZE
def h(key):
    return key % SIZE              # hash function: remainder method
for k in [34, 16, 2, 93, 80, 77]:
    table[h(k)] = k
print(table)
def hash_search(key):
    return table[h(key)] == key    # only ONE look-up
print(93, hash_search(93))
print(45, hash_search(45))
[80, None, 2, 93, 34, None, 16, 77, None, None] 93 True 45 False

Collisions

Python
# Collision: two keys get the same position
SIZE = 10
print('h(23) =', 23 % SIZE, ' h(43) =', 43 % SIZE)
# Simple fix: chaining – keep a small list at every slot
table = [[] for _ in range(SIZE)]
for k in [23, 43, 15, 33]:
    table[k % SIZE].append(k)
print(table[3], table[5])
h(23) = 3 h(43) = 3 [23, 43, 33] [15]

Hash table के रूप में Python dictionary

Python
# Python dict is a ready-made hash table
phone = {'Asha': '98110', 'Ravi': '98220', 'Neha': '98330'}
name = 'Ravi'
print(name, '->', phone.get(name, 'not found'))
print('Zoya ->', phone.get('Zoya', 'not found'))
Ravi -> 98220 Zoya -> not found

सारांश

LinearBinaryHashing
Needs sorted data?NoYesNo
Worst casen comparisonsabout log₂n1 (no collision)
Best forSmall / unsorted listsLarge sorted listsFast look-up by key

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Binary search की शर्त क्या है?
List sorted होनी चाहिए। Binary search key की तुलना बीच वाले element से करता है और हर बार आधी list छोड़ देता है।
1000 elements के लिए binary search को कितने comparisons चाहिए?
ज़्यादा से ज़्यादा लगभग 10, क्योंकि 2^10 = 1024। Linear search को 1000 तक लग सकते हैं।
Hashing में collision क्या है?
जब दो अलग keys एक ही hash value (position) दें। इसे chaining या probing जैसे तरीकों से सुलझाते हैं।
← Class 12 Computer Science
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicData और Statistics समझना आपकी प्रगति: 13 / 26 (50%) · Lesson 14 / 26 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें