Linear or Sequential Search Introduction and function/ algorithm/ program

Linear/sequential search:- 
"It is a simple and easy technique of searching in which an item is searched in given data structure / list one-by-one in linear order until item is found or all the elements of data structure/ list is compared."
"यह सर्चिंग की एक सामान्य एवं सरल युक्ति है जिसमे किसी आइटम को डाटा स्ट्रक्चर/लिस्ट में, एक के बाद एक श्रेणी क्रम में खोजा जाता है।" 
It means, First of all given item is compared with first element of data structure/list and if it is matched then we return position or location of the element.Otherwise, item will be compared with second element of data structure/list and this process will continue... 
Now, If item is not present in given data structure then searching process is called unsuccessful. 
Linear search is more effective for searching an item in the unordered list of fewer elements.
its worst/ average case time complexity is O(n) and space complexity is O(1). 
अर्थात सर्वप्रथम आइटम की तुलना, डाटा स्ट्रक्चर/लिस्ट के पहले एलिमेंट से की जाती है। यदि यह आइटम के बराबर है तब उसकी लोकेशन / पोजीशन प्रदान की जाती है अन्यथा आइटम की तुलना दुसरे एलिमेंट से की जाती है यह प्रक्रिया चलती रहती है...
अब यदि आइटम डाटा स्ट्रक्चर / लिस्ट में उपलब्ध नहीं है तब लीनियर सर्च को असफल माना जाता है। 
लीनियर सर्च तब अत्यधिक प्रभावी होती है, जब हमे सीमित संख्या में एलिमेंट रखने वाले अव्यवस्थित स्ट्रक्चर/लिस्ट में किसी आइटम को खोजना हो।  
इसकी वर्स्ट एवं एवरेज टाइम कॉम्प्लेक्सिटी O(n) तथा स्पेस कॉम्प्लेक्सिटी O(1) होती है।  
 
Linear Search function/ algorithm/ program:-

File Name:- ls.cpp

#include<iostream.h>
#include<conio.h>
#define N 5
void ls(){
int list[N],item,i;
clrscr();
cout<<"Enter Elements of List"<<endl;
for(i=0;i<N;i++){
cout<<"Enter "<<i+1<<" element=";
cin>>list[i];
}
cout<<"Enter Item to search=";
cin>>item;
for(i=0;i<N;i++){
if(item==list[i]){
cout<<item<<" is found at "<<i<<"th index and "<<i+1<<"th position"<<endl;
return;
}
}
cout<<item<<" is not found"<<endl<<"Search Unsuccessful"<<endl;
return;
}
// main function
void main(){
clrscr();
cout<<"Linear Search"<<endl;
ls();
getch();
}


File Name:- ls.c

#include<stdio.h>
#include<conio.h>
#define N 5
void ls(){
int list[N],item,i;
clrscr();
printf("Enter Elements of List\n");
for(i=0;i<N;i++){
printf("Enter %d element=",i+1);
scanf("%d",&list[i]);
}
printf("Enter Item to search=");
scanf("%d",&item);
for(i=0;i<N;i++){
if(item==list[i]){
printf("%d is found at %d index and %d position\n",item,i,i+1);
return;
}
}
printf("%d is not found\n Search Unsuccessful\n",item);
return;
}
// main function
void main(){
clrscr();
printf("Linear Search\n");
ls();
getch();
}

No comments:

Post a Comment

Stack Data Structure, Push, Pop and Peek Operations , Applications of Stack

Stack is a linear data structure. It is collection of ordered data elements. It is also known as LIFO system (last in first out). It means i...