Linear Search in Python in Hindi – List और Tuple पर Linear Search Algorithm पूरी जानकारी
Table of Contents
Linear Search, python में data search करने का सबसे पहला और सबसे basic तरीका है। जब भी हम coding सीखना शुरू करते हैं, searching algorithms में सबसे पहले यही topic पढ़ाया जाता है।
Linear Search का सीधा मतलब है कि हम list या tuple के हर element को एक-एक करके check करते हैं जब तक हमें target value न मिल जाए।
इसे Sequential Search भी कहा जाता है, क्योंकि यह elements को उसी sequence में check करता है जिस order में वो store हैं।
यह algorithm किसी भी तरह की collection पर काम करता है — चाहे वो sorted हो या unsorted।
Python में यह concept lists और tuples दोनों पर लगभग एक जैसे तरीके से काम करता है, क्योंकि दोनों ही indexable sequences हैं।
Exam की नजर से देखा जाए तो Linear Search एक ऐसा topic है जो data structures और algorithms दोनों subjects में common रूप से पूछा जाता है।
यह beginners के लिए इसलिए भी फायदेमंद है क्योंकि इसे समझने के लिए ज्यादा complex logic की जरूरत नहीं पड़ती।
Introduction to Linear Search in Hindi
अगर आप कभी अपने बैग में कोई एक खास किताब ढूंढते हैं, तो आप एक-एक करके हर किताब को check करते हैं जब तक वो किताब मिल न जाए। Linear Search ठीक इसी तरह काम करता है — बस यहाँ किताबों की जगह list या tuple के elements होते हैं।
Computer science में Linear Search एक ऐसा searching algorithm है जो किसी collection (list, array, tuple) में किसी particular value को ढूंढने के लिए हर element को शुरू से लेकर आखिर तक एक-एक करके check करता है। जैसे ही target value match होती है, search वहीं रुक जाती है और element का index return कर दिया जाता है।
यह SVG दिखा रहा है कि Linear Search list के हर index को index 0 से शुरू करके एक-एक करके कैसे check करता है, और target value (88) मिलते ही search कैसे रुक जाती है।
Linear Search Algorithm in Hindi
अब बात करते हैं कि यह algorithm असल में step by step कैसे काम करता है। Exam में अक्सर algorithm के steps लिखने के लिए पूछा जाता है, इसलिए इसे ध्यान से समझना जरूरी है।
Steps of Linear Search Algorithm
Step 1:
सबसे पहले list (या tuple) का first index यानी index 0 select किया जाता है।Step 2:
current index के value को target value से compare किया जाता है।Step 3:
अगर value match हो जाए, तो उस index को return कर दिया जाता है और search वहीं खत्म हो जाती है।Step 4:
अगर value match नहीं होती, तो अगले index पर move किया जाता है।Step 5:
यह process तब तक repeat होता है जब तक या तो value मिल जाए, या पूरी list/tuple खत्म हो जाए।Step 6:
अगर पूरी list check करने के बाद भी value नहीं मिलती, तो -1 (या "Not Found" message) return कर दिया जाता है।
Pseudocode of Linear Search
Exam preparation के लिए यह pseudocode याद रखना काफी helpful होता है:
ALGORITHM LinearSearch(array, target):
for i from 0 to length(array) - 1:
if array[i] == target:
return i // element मिल गया, index return करो
return -1 // element नहीं मिला
Linear Search on Lists and Tuples in Python
Python में list और tuple दोनों ही ordered sequences हैं, यानी दोनों में हर element का एक fixed index होता है। इसी वजह से दोनों पर Linear Search लगभग same तरीके से implement होता है — बस tuple immutable होती है, इसलिए search के दौरान उसमें कोई changes नहीं किए जा सकते।
Linear Search on List in Python
नीचे दिया गया function एक simple list में linear search perform करता है:
def linear_search_list(data, target):
for index in range(len(data)):
if data[index] == target:
return index
return -1
marks = [56, 78, 90, 45, 67, 88]
result = linear_search_list(marks, 90)
if result != -1:
print(f"Element index {result} par mil gaya")
else:
print("Element list me maujood nahi hai")
इस code में range(len(data)) हमें list की हर position पर जाने देता है और data[index] == target check हर बार value compare करता है। जैसे ही match मिलता है, function उसी index को return करके रुक जाता है — इसे ही early exit कहते हैं और यह real search का सबसे important फायदा है।
Linear Search on Tuple in Python
Tuple पर search करने का logic बिल्कुल same रहता है, बस data type tuple हो जाता है:
def linear_search_tuple(data, target):
for index in range(len(data)):
if data[index] == target:
return index
return -1
cities = ("Delhi", "Mumbai", "Bhopal", "Chennai", "Pune")
result = linear_search_tuple(cities, "Chennai")
if result != -1:
print(f"'Chennai' index {result} par mila")
else:
print("City tuple me nahi hai")
ध्यान दीजिए कि function का logic बिल्कुल वही है जो list वाले example में था, क्योंकि Python में tuple भी indexing और iteration support करता है, बस उसमें elements को बाद में modify नहीं किया जा सकता।
Exam Tip: अगर आपसे पूछा जाए कि "क्या Linear Search list और tuple दोनों पर काम करता है?", तो जवाब है — हाँ, क्योंकि दोनों indexable और iterable sequences हैं। सिर्फ tuple immutable होने की वजह से उसमें searching के दौरान कोई element replace या delete नहीं किया जा सकता।
Finding All Occurrences (Multiple Matches)
कई बार target value list या tuple में एक से ज्यादा बार भी हो सकती है। ऐसे में हमें सारे matching indexes चाहिए होते हैं, ना कि सिर्फ पहला वाला:
def linear_search_all(data, target):
positions = []
for index, value in enumerate(data):
if value == target:
positions.append(index)
return positions
numbers = (10, 25, 30, 25, 40, 25)
found = linear_search_all(numbers, 25)
print("Value 25 in indexes par mili:", found)
यहाँ enumerate(data) का इस्तेमाल करने से हमें एक साथ index और value दोनों मिल जाते हैं, जिससे code छोटा और साफ बनता है।
Python के Built-in तरीके (in, index)
असल exam या project में हमेशा manual function लिखना जरूरी नहीं होता। Python पहले से कुछ built-in tools देता है जो internally linear search जैसा ही logic use करते हैं:
fruits = ["apple", "banana", "mango", "grapes"]
# 'in' keyword se check karna ki value maujood hai ya nahi
print("mango" in fruits) # Output: True
# index() method se position pata karna
print(fruits.index("grapes")) # Output: 3
# tuple ke sath bhi same tarike se kaam karta hai
fruit_tuple = ("apple", "banana", "mango")
print("banana" in fruit_tuple) # Output: True
इंटरव्यू या exam में अक्सर पूछा जाता है कि in operator और .index() method internally किस algorithm पर based हैं — जवाब है, दोनों internally sequential यानी linear तरीके से ही elements को check करते हैं।
Characteristics of Linear Search in Hindi
1. Sequential Checking
Linear Search हमेशा elements को एक निश्चित order में, यानी शुरू से आखिर तक, एक-एक करके check करता है। यह कभी भी बीच से या random order में elements को नहीं देखता।
2. No Sorting Required
Binary Search जैसी algorithms को काम करने के लिए data sorted होना जरूरी होता है, लेकिन Linear Search किसी भी unsorted list या tuple पर बिना किसी extra tayyari के सीधे काम कर सकता है।
3. Works on List and Tuple Both
चूंकि Python में list और tuple दोनों indexable sequences हैं, इसलिए एक ही तरह का linear search logic दोनों data types पर बिना किसी बदलाव के apply किया जा सकता है।
4. Simple Implementation
इस algorithm को समझना और code करना बहुत आसान है। इसीलिए ज्यादातर beginners searching algorithms सीखने की शुरुआत Linear Search से ही करते हैं।
5. Time Complexity
Worst case में Linear Search को पूरी list को scan करना पड़ सकता है, इसलिए इसकी time complexity O(n) होती है, जहाँ n elements की total संख्या है।
Time and Space Complexity of Linear Search
Exam में complexity से जुड़े questions बहुत common होते हैं, इसलिए यह table ध्यान से समझ लें:
| Case | Description | Time Complexity |
|---|---|---|
| Best Case | Target value पहले ही index (index 0) पर मिल जाए | O(1) |
| Average Case | Target value list के बीच के किसी index पर मिले | O(n) |
| Worst Case | Target value आखिरी index पर मिले या मिले ही ना | O(n) |
Space complexity की बात करें तो Linear Search किसी extra memory का इस्तेमाल नहीं करता (सिर्फ multiple occurrences ढूंढने वाले version में एक छोटी list बनानी पड़ती है), इसलिए इसकी space complexity लगभग O(1) मानी जाती है।
यह SVG दिखा रहा है कि जैसे-जैसे list या tuple में elements की संख्या (n) बढ़ती है, worst case में Linear Search का समय भी उसी अनुपात में बढ़ता है — यही O(n) complexity को दर्शाता है।
Advantages of Linear Search in Python in Hindi
Implementation बहुत simple है, बस एक loop से काम चल जाता है।
Data को sort करने की कोई जरूरत नहीं होती, चाहे list random order में हो या ना हो।
यह list और tuple दोनों data types पर बिना किसी extra effort के काम करता है।
छोटी size की lists या tuples के लिए यह काफी fast और efficient रहता है।
Beginners के लिए algorithm की basic समझ बनाने का यह सबसे अच्छा तरीका है।
Disadvantages of Linear Search in Python in Hindi
बड़ी size की lists या tuples के लिए यह काफी slow हो जाता है, क्योंकि worst case में हर element check करना पड़ता है।
Time complexity O(n) होने की वजह से यह Binary Search जैसी algorithms (जिनकी complexity O(log n) होती है) से कम efficient है।
Large datasets के real-world applications, जैसे databases या search engines, में इसे अकेले इस्तेमाल करना practical नहीं होता।
अगर target value list में बार-बार ढूंढनी हो, तो हर बार scratch से search करना resources waste कर सकता है।
FAQs
in operator और .index() method internally sequential यानी linear तरीके से ही elements check करते हैं।