Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

List->Static list->Key Search

Key Search:-
Well, KEY is just a particular element you want to search in a LIST. It might be there or might not be even. If its there you are supposed to return the position (i.e its INDEX value) or else say "NOT there".

See, I might be tackling this very lightly as per you, but don't worry SEARCHING a Key has been covered properly in my last section of this tutorial.

For now, we will discuss, what you call a "Sequential Search".
You can make out easily what it is? Start searching from the first element in the LIST, check for every next element till the time you find the KEY value or the LIST finishes. I will take help of an algorithm again...

Algorithm:-
Step-1: Initialize your list's INDEX to 0
Step-2: Check whether the list element for the current INDEX matches the KEY
Step-3: If it matches -- you say "found" and quit -- or else -- go to step-4
Step-4: Increment the INDEX by 1 till < LISTSIZE of the list
Step-5: Go to step-2

Let us see its C implementation:-

int list[listsize];
void keysearch(int key)
{
int index=0, found=0;
while(index<listsize)
  {
  if(list[index]==key) { found=1;printf("Found"); break;}
  index=index+1;
  }
if(found==0)printf("Not found");
}

Note:- This searching would take a worst case of N comparisons to search a KEY in a LIST of size N.


Related Operations:
Addition and Deletion of elements
Traversing of list
Searching for a particular Key
Sorting of list in ascending and descending order

 

Index || Doubts / Clarifications || Related Topics || Web Links