You are on page 1of 24

Searching

Sequential Search
O (n)

• A sequential search of a list/array begins at


the beginning of the list/array and continues
until the item is found or the entire list/array
has been searched

2
Sequential Search
int SeqSearch(int x[ ], int n, int item)
{
for(i=0;i<n;i++)
{
if(x[i]==item)
return 1;
else
return 0;
}
return 0;
}

Data Structures Using C 3


Search Algorithms
Suppose that there are n elements in the array. The following expression
gives the average number of comparisons:

It is known that

Therefore, the following expression gives the average number of comparisons


made by the sequential search in the successful case:

Data Structures Using C 4


Search Algorithms

Data Structures Using C 5


Binary Search
O(log2 n)

• A binary search looks for an item


in a list using a divide-and-
conquer strategy

Data Structures Using C 6


Binary Search
– Binary search algorithm assumes that the items in
the array being searched are sorted
– The algorithm begins at the middle of the array in a
binary search
– If the item for which we are searching is less than
the item in the middle, we know that the item won’t
be in the second half of the array
– Once again we examine the “middle” element
– The process continues with each comparison cutting
in half the portion of the array where the item might
be

Data Structures Using C 7


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is 7 = midpoint key? NO.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is 7 < midpoint key? YES.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the target in the area before midpoint.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target = key of midpoint? NO.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target < key of midpoint? NO.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target > key of midpoint? YES.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the target in the area after midpoint.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint.


Is target = midpoint key? YES.
Binary search
• It is used for finding a given element in a sorted
sequence stored in an array.
int binsrch(int a[], int x, int low, int high)
{
int mid;
if (low > high) e.g. 1 4 7 8 10 11 14
return(-1);
while (high >= low){
mid = (low+high)/2;
if (x == a[mid])
return(mid);
if (x < a[mid])
high = mid-1;
else Search 6
low = mid+1;
}
return(-1); /* */
} 3 -19
Recursive binary search
int binsrch(int a[], int x, int low, int high)
{
int mid;
if (low > high)
return(-1);
mid = (low+high) /2;
if(x == a[mid])
return mid
else
if (x < a[mid])
binsrch(a, x, low, mid-1)
else
binsrch(a, x, mid+1, high));
} /* end binsrch */

3 -20
Binary Search: Example

Table : Values of low, high and middle and the number of comparisons
for search item 89

Iteration low high mid List[mid] Number of Comparisons


1 0 11 5 39 2
2 6 11 8 66 2
3 9 11 10 89 1 (found)

Data Structures Using C 21


Binary Search

Data Structures Using C 22


Binary Search
• Benefit
– Much more efficient than the linear search
• Disadvantage
– Arrary elements be in sorted order
Efficiency of binary search
Maximum Maximum binary
sequential searches searches
# of values necessary necessary
10 10 4
100 100 7
1,000 1,000 10
5,000 5,000 13
10,000 10,000 14
50,000 50,000 16
100,000 100,000 17
1,000,000 1,000,000 20
10,000,000 10,000,000 24
1,000,000,000 1,000,000,000 30
With the incredible speed of today’s computers, a binary
search becomes necessary only when the number of
names is large.

You might also like