C Program for Linear or Sequential Search लीनियर या सेक्वेंशिअल सर्च
Linear Search is a simple searching technique used to find an element in an array. It checks each element one by one until the required item is found or the array ends.
Linear Search एक सरल Searching Technique है, जिसका उपयोग Array में किसी Element को खोजने के लिए किया जाता है। इसमें प्रत्येक Element को एक-एक करके Check किया जाता है, जब तक Element मिल न जाए या Array समाप्त न हो जाए।
C Program
#include <stdio.h>
#define N 5
int main()
{
int list[N], i, item;
printf("Enter 5 Array Elements:\n");
for (i = 0; i < N; i++)
{
scanf("%d", &list[i]);
}
printf("Enter an item to search: ");
scanf("%d", &item);
for (i = 0; i < N; i++)
{
if (item == list[i])
{
printf("Searching is Successful\n");
printf("%d is found at Index %d and Position %d\n",
item, i, i + 1);
return 0;
}
}
printf("Searching is Unsuccessful\n");
printf("%d is not found\n", item);
return 0;
}Output (आउटपुट)
Enter 5 Array Elements:
10 20 30 40 50
Enter an item to search: 30
Searching is Successful
30 is found at Index 2 and Position 3Explanation (व्याख्या)
Array: Stores five integer elements.
Array: पाँच Integer Elements को Store करता है।Input: The user enters the elements and the item to search.
Input: User Array के Elements और Search किए जाने वाले Item को Input करता है।Search: The
forloop checks each element usingif.
Search:forLoop औरifCondition की सहायता से प्रत्येक Element को Check किया जाता है।Result: If the item is found, its index and position are displayed. Otherwise, a not-found message is shown.
Result: Element मिलने पर उसका Index और Position Display होता है। नहीं मिलने पर Not Found का Message Display होता है।
Remember (ध्यान रखें):
Array Index starts from 0, but Position starts from 1.
Array का Index 0 से शुरू होता है, जबकि Position 1 से शुरू होती है।
Linear Search is easy to understand and works with both sorted and unsorted arrays.
Linear Search समझने में आसान है और Sorted तथा Unsorted दोनों प्रकार के Arrays में काम करता है।
Comments
Post a Comment