Linear Search Algorithm hindi, searching in data structure in hindi, linear search in hindi, difference between linear search and binary search in hindi,
- Linear search algorithm in hindi
- Complexity of algorithm in hindi
- linear search c program
- linear search java program
- linear search c# program
Contents
show
Linear Search Algorithm in hindi
Linear search सबसे सरल खोज एल्गोरिदम है और जिसे अक्सर अनुक्रमिक (sequential) खोज कहा जाता है। इस प्रकार की खोज में, हम बस सूची को पूरी तरह से traverse कर लेते हैं और सूची के प्रत्येक तत्व को उस आइटम से मिलाते हैं जिसका स्थान ढूंढा जाना है। यदि मैच मिला तो आइटम का स्थान दिया जाता है अन्यथा एल्गोरिथ्म NULL वापस आता है।
Linear search का उपयोग ज्यादातर unordered list को खोजने के लिए किया जाता है जिसमें items sort नहीं किए जाते हैं। linear search का एल्गोरिथ्म निम्नानुसार दिया गया है।
Algorithm
- LINEAR_SEARCH(A, N, VAL)
- Step 1: [INITIALIZE] SET POS = -1
- Step 2: [INITIALIZE] SET I = 1
- Step 3: Repeat Step 4 while I<=N
- Step 4: IF A[I] = VAL
SET POS = I
PRINT POS
Go to Step 6
[END OF IF]
SET I = I + 1
[END OF LOOP] - Step 5: IF POS = -1
PRINT ” VALUE IS NOT PRESENTIN THE ARRAY ”
[END OF IF] - Step 6: EXIT
Complexity of algorithm
Complexity | Best Case | Average Case | Worst Case |
---|---|---|---|
Time | O(1) | O(n) | O(n) |
Space | O(1) |
C Program
- #include<stdio.h>
- void main ()
- {
- int a[10] = {10, 23, 40, 1, 2, 0, 14, 13, 50, 9};
- int item, i,flag;
- printf(“\nEnter Item which is to be searched\n”);
- scanf(“%d”,&item);
- for (i = 0; i< 10; i++)
- {
- if(a[i] == item)
- {
- flag = i+1;
- break;
- }
- else
- flag = 0;
- }
- if(flag != 0)
- {
- printf(“\nItem found at location %d\n”,flag);
- }
- else
- {
- printf(“\nItem not found\n”);
- }
- }
Output:
Enter Item which is to be searched 20 Item not found Enter Item which is to be searched 23 Item found at location 2
Java Program
- import java.util.Scanner;
- public class Leniear_Search {
- public static void main(String[] args) {
- int[] arr = {10, 23, 15, 8, 4, 3, 25, 30, 34, 2, 19};
- int item,flag=0;
- Scanner sc = new Scanner(System.in);
- System.out.println(“Enter Item ?”);
- item = sc.nextInt();
- for(int i = 0; i<10; i++)
- {
- if(arr[i]==item)
- {
- flag = i+1;
- break;
- }
- else
- flag = 0;
- }
- if(flag != 0)
- {
- System.out.println(“Item found at location” + flag);
- }
- else
- System.out.println(“Item not found”);
- }
- }
Output:
Enter Item ? 23 Item found at location 2 Enter Item ? 22 Item not found
C# Program
- using System;
- public class LinearSearch
- {
- public static void Main()
- {
- int item, flag = 0;
- int[] a= {10, 23, 5, 90, 89, 34, 12, 34, 1, 78};
- Console.WriteLine(“Enter the item value”);
- item = Convert.ToInt32(Console.ReadLine());
- for(int i=0;i<10;i++)
- {
- if(item == a[i])
- {
- flag = i+1;
- break;
- }
- else
- flag = 0;
- }
- if(flag != 0 )
- {
- Console.WriteLine(“Item Found at Location “ + flag);
- }
- else
- Console.WriteLine(“Item Not Found”);
- }
- }
Output:
Enter the item value 78 Item Found at Location 10 Enter the item value 22 Item not found