0% found this document useful (0 votes)
34 views15 pages

Search in C

Linear search examines each element of an unsorted array sequentially until a match is found. It has O(n) complexity. Binary search works on a sorted array by dividing the search space in half at each step based on comparing the target to the middle element. This has O(log n) complexity, making it more efficient than linear search for large arrays.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views15 pages

Search in C

Linear search examines each element of an unsorted array sequentially until a match is found. It has O(n) complexity. Binary search works on a sorted array by dividing the search space in half at each step based on comparing the target to the middle element. This has O(log n) complexity, making it more efficient than linear search for large arrays.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Linear and Binary Search

Searching Arrays Linear search


small arrays unsorted arrays

Binary search
large arrays sorted arrays

Linear Search Algorithm

Start at first element of array. Compare value to value (key) for which you are searching Continue with next element of the array until you find a match or reach the last element in the array.

Note: On the average you will have to compare the search key with half the elements in the array.

Linear Search Brute force approach


Start at the beginning and examine each element in turn. It takes linear time in both the worst and average cases.

Sometimes called sequential search

6 5 4 3 2 1

21 13 8 5 3 2

target

How can we locate a specific element in an array?

Pseudo-code of Linear Search For Array


int LinearSearch( int key, int[ ] db, int n ) { int index;

for( index=0; index<n; index++ ) {


if( db[index]==key ) return index; } return 1; } 6 5 4 3 2 1

21 13 8 5 3 2

target

An Array db stores integer numbers


Search for first value 8 in this array If found, return index

Otherwise, return 1

Complexity of Linear Search Suppose there are n records

In the best case, the target is the first entry. It only takes 1 step
In the worst case, the target is the last entry, or it cannot be found.
Sequential search has to scan all n records and takes totally n steps.

Cost: O(n)

Binary Search If the records are sorted by the key, we can do better.

Think about how you look up a word in English dictionary where the words are sorted alphabetically.
If the target is present in the list, it must be one between low and high

A B C
low=0

M
middle=12

X Y Z
high=25

Take a look at the middle one. middle = (low+high) / 2

Binary Search: Case 1 Assume target is M

A B C
low=0

M
middle

X Y Z
high=25

If the target is the same as the key value in the middle position, we have found the record. Return the value of middle

Binary Search: Case 2 Assume target is E

A B C
low=0

L M
high=11

X Y Z

If target < the key value in the middle position, we know that it can not be found in the right half. We only need to search in the left half. high = middle - 1;

Binary Search: Case 3 Assume target is P

A B C

M N
low=13

X Y Z
high=25

If target > the key value in the middle position, we know that it can not be found in the left half. We only need to search the right half. low = middle + 1;

Binary Search Starting our search in the middle with a sorted array.
A constant amount of work allows us to divide the data in half.

Pseudo-code of Binary Search


int BinarySearch(char target, char[] array) { int low=0, high = array_length-1, middle; while (low <= high) { middle = (low+high)/2; if( array[middle]==target ) return middle; else if( array[middle]<target ) low = middle + 1; else high = middle - 1; } return -1; }

Keep splitting the array in half until result is found Array must initially be ordered

Consider a Dictionary

Looking up a word in a dictionary In what sense is it like a binary search? In what sense not? What is the complexity??

Complexity For Binary Search Suppose there are n records In the best case, the target is the middle entry. It only takes 1 step In the worst case, it will take log2n + 1 steps. Cost: O(logN)

Summary Linear search is easy to implement


Basically, just a for loop Has O(n) complexity for n stored items

More efficient than binary search for small n

Binary search is harder to implement but more efficient


Requires data to be sorted

Has O(log n) complexity for n stored items Frequently used for reasonably-sized data sets - E.g. more than 10-20 stored items

You might also like