Tuesday, 25 June 2013

Algorithm: Pseudo-code to find a particular item form a list of items stored in a linear array.

a nonempty linear array DATA with N elements in it and a ITEM of information / element are given. a linear search algorithm is to find the ITEM in DATA and if so then location LOC of ITEM in DATA to print out.

  1. set K :=1 and LOC:=0. [ initialize ]
  2. while LOC=0 and K<=N, repeat step 3 and 4.
  3. if ITEM = DATA[K], then set LOC :=K.
  4. set K:=K+1. [increment counter ] [end of step 2 loop ]
  5. If : LOC=0 then:  Write: ITEM isn't in the array DATA.                                                       Else: Write: LOC is the location of ITEM..
  6. Exit.



No comments:

Post a Comment