7_Arrays_1_linear Search
7_Arrays_1_linear Search
• Since an array stores all its data elements in consecutive memory locations,
storing just the base address, that is the address of the first element in the
array, is sufficient. The address of other data elements can simply be
calculated using the base address.
Address Calculation – An Example
(16 bit machine)
Searching in Arrays
• A technique to check
Searching
for existence of a
data in a list.
• Searching involves Linear Binary
Search Search
comparing the data
to be searched Vs the Two approach of searching
elements in the array. with an array
Element Searched =2
Searching for an element in an array
2
2
Compare 2
“No Match” Compare
“No Match” Compare
“ Match Found”
Search Stops
5 10 2 14 7
23
23
23
23 23
Compare Compare
“No Match” Compare “No Match”
Compare Compare
“No Match”
“No Match” “No Match”
Search Stops
5 10 2 14 10
}
}
if (found >= 1)
printf(" Element %d is present %d times in the array\n",key,found);
else
printf("Element is not present in the array\n");
}