0% found this document useful (0 votes)
5 views

7_Arrays_1_linear Search

Uploaded by

ayusssssh100
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

7_Arrays_1_linear Search

Uploaded by

ayusssssh100
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Arrays

Array Elements Storage


Base
• Arrays Elements are int A[6]; Address
stored in contiguous A[0] 1000
memory location. A[1] 1004
• The address of the first -- 1008 Addresses at
element is the base -- 1012 each location
address. -- 1016
A[5] 1020
• All subsequent address main()
{
is based on the data int a[5], i;
type of the array. For 32 bit machine for(i=0;i<5;i++)
(4 Bytes=4x4=16bits) printf(“\n%u”, &a[i]);
Memory Address is usually written in }
hexadecimal as 16 bits will have 4 digit
representation in hex
Calculating the Address of 1D Array Elements
Address of A[k] = Base_Address(A) + w(k – Lower_Bound)
• Here, A is the array, k is the index of the element of which we have to
calculate the address, Base_Address is the base address of the array A, and
w is the size of one element in memory, for example, size of int is 2 bytes.

• 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

Both checks for existence


of a data.
Linear Search
• A linear search, also known as a sequential search, is a
method of finding an element within a list.
• It checks each element of the list sequentially until a match is
found or the whole list has been searched.
Searching for an element in an array

Element Searched =2
Searching for an element in an array

1. Compare search_element and arr[i]


2. Both are not equal
3. Advance to next index
Searching for an element in an array
5 10 2 14 7

2
2
Compare 2
“No Match” Compare
“No Match” Compare
“ Match Found”

Search Stops

Worst complexity: O(n)


Average complexity: O(n)
Average performance: O(n/2)
Alternative Case – Let element to be searched be 23

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

Worst complexity: O(n)


Average complexity: O(n)
Average performance: O(n/2)
#include <stdio.h> /* Linear search begins */
void main(){ for (i = 0; i < num ; i++) {
int num; if (key == array[i] ){
int i, key, found = 0; found = 1;
printf("Enter the number of elements break;
"); }
scanf("%d", &num); }
int array[num]; if (found == 1)
printf("Enter the elements \n"); printf("Element is present in the
for (i = 0; i < num; i++){ array
scanf("%d", &array[i]); at position %d",i+1);
} else
printf("Enter the element to be printf("Element is not present in the
searched "); array\n");
scanf("%d", &key); }
Improvisation on Linear Search
Let the Array A[5] be :-

5 10 2 14 10

Write a program to search for an element and display – “Found” if


successful else “Not Found”.
Also, display how many times and what locations the element is present

Let the element to be searched is 10


O/P: “Found”
The element 10 is available in 2 places with Index Position 1 and 4
/* Linear search begins */
for (i = 0; i < num ; i++) {
if (key == array[i] ){
printf("\n Found %d at position %d\n",key,i+1);
found = found+1;

}
}

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");
}

You might also like