Tuesday, 14 December 2010

Algorithm for searching element in an array with position

Algorithm for search an element in an Single Dimensional Array:

1. Take formal parameters of type 1d array ref of int, value to be searched
2. Declare two variables of type int: (e,ctr)
   one is for count, second is for to store found value.
3. Declare and Initialize variable (ex: m) with 0 for location.
4. Loop starts:
   this loop should execute a.length times
5. Compare each element with search element (n)
6. If step 5 is true
    (i)store that element into variable (e)
    (ii)store index value into m (to display the search value at particular array index location
    (iii) increment the count for (this statement is for to find duplicate values
7. Repeat the step 4 to step 6 until i becomes array size
8. compare if counter (ctr) is greater than or equal to 1
   then
    print "Value Found at this location"+arrayIndex+1"
   if counter (ctr) is equal to 0
    then
      print " element +is not found"
9. compare the counter (ctr) is greater than or equal to 2
   then
    print "search element+ found +ctr+ times

10 . End

No comments:

Post a Comment