Important Notice:

Linear Search

Linear Search

4 views 2 min read

Linear Search :-

Linear Search एक simple searching technique है जिसमें किसी List, Tuple, String या अन्य sequential data के elements को एक-एक करके क्रम से check किया जाता है, जब तक कि required element मिल न जाए या सभी elements check न हो जाएँ।

इसे Sequential Search भी कहा जाता है।

Linear Search में निम्न steps होते हैं:

  1. Data के first element से शुरुआत होती है।
  2. Current element को search element से compare किया जाता है।
  3. अगर दोनों समान हैं → Element Found।
  4. अगर समान नहीं हैं → अगले element पर जाते हैं।
  5. यह process element मिलने तक चलता है।
  6. अगर पूरा data check हो गया और element नहीं मिला → Element Not Found।

Example-

numbers = [10, 20, 30, 40, 50]

search = 40

for num in numbers:
    if num == search:
        print("Element Found")
        break
else:
    print("Element Not Found")
 
Output:
Element Found
 
Explanation :-

यहाँ:

search = 40

इसका अर्थ है कि हमें 40 को search करना है।

Program elements को इस क्रम में check करेगा:

10 → 20 → 30 → 40

जब 40 मिल जाता है, तो:

break

के कारण loop समाप्त हो जाता है।
 
IN ENGLISH -
 
Linear Search :-

Linear Search is a simple searching technique in which the elements of a List, Tuple, String, or other sequential data are checked one by one in sequence until the required element is found or all elements have been checked.

It is also called Sequential Search.

The following steps are involved in Linear Search:

Searching starts from the first element of the data.
The current element is compared with the search element.
If both are equal → Element Found.
If they are not equal → Move to the next element.
This process continues until the element is found.
If all elements have been checked and the element is not found → Element Not Found.

Example-

numbers = [10, 20, 30, 40, 50]

search = 40

for num in numbers:

if num == search:

    print("Element Found")

    break

else:

print("Element Not Found")

Output:

Element Found

Explanation :-

Here:

search = 40

It means that we want to search for 40.

The program will check the elements in the following order:

10 → 20 → 30 → 40

When 40 is found:

break

The loop terminates because of the break statement.

Related Notes