Anda di halaman 1dari 14

Searching

Computer systems are often used to store


large amounts of data from which individual
records must be retrieved according to some
search criterion.
Thus the efficient storage of data to facilitate
fast searching is an important issue.
Searching
The simplest, most general, and least efficient
search structure is merely an unordered
sequential list of all the items
Locating the desired item in such a list, by the
linear search method, inevitable requires a
number of operations proportional to the
number of n items, in the worst case as well as
in the average case.
Searching
Useful search data structures allow faster
retrieval; however they are limited to queries
of some specific kind
Searching refers to the operation of finding
the location of item in a given list.
Basically there are two different searching
methods
1. Linear search
2. Binary search
Linear Search
Linear search is to begin the search in the first
available records and proceeding to the next
available record repeatedly until we find the
target record or can conclude that it will not
be found.
This technique must be used when the
records are stored without any consideration
given to order or when the storage medium
lacks any type of direct access facility (Eg.
magnetic tape)
Linear Search
1. Algorithm linear search(a,n,x)
2. {
3. //a is an array with n elements
4. //x is an element to be searched
5. //found is a variable, found=true if the search is successful. It is
false otherwise
6. Found=false
7. i=1
8. Repeat
9. if a(i)=x then
10. Found = true
11. Else
12. i=i+1
13. Until (found=true or i+n)
14. }
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++ 6


Search Algorithms

Data Structures Using C++ 7


Linear Search
Analysis of time complexity
In the worst case we must search through the
entire array a.
In this case the algorithm requires f(n)=n
comparisons
So the time complexity is O(n)
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++ 9


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 wont
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++ 10


Binary Search
1. Algorithm binary search (a,n,x)
2. {
3. //a is an array with n elements
4. //x is an element to be searched
5. Found=false
6. Low=1
7. High=n
8. Repeat
9. Mid=(low+high)/2
10. If (x<a(mid)) then
11. high=mid-1
12. Else if (x>a(mid)) then
13. Low=mid+1
14. Else
15. Found=true
16. Until (found=true or high<low)
17. }
Binary Search
Suppose array contain 9 elements.
7,11,33,55,77,80,85,90,99. We apply the binary
search for the value x=33
Solution
1. Initially low=1 and high=9
mid=(1+9)/2=5
So, x(mid)=x(5)=77
2. Since 33<77, high value changed by
high=mid-1=4
Now, mid=(1+4)/2=2
So, x(mid)=x(2)=11
Binary Search
3. Since 33>11, low value changed by
low=mid+1=3
Now, mid=(3+4)/2=3
So, x(mid)=x(3)=33
Now, we have formed the item in location mid=3
Binary search requires an ordering comparison;
linear search only requires equality comparisons
Binary search has complexity O(log n); linear
search has complexity O(n) as discussed earlier
Binary search requires random access to the
data; linear search only requires sequential
access
Binary Search
Distinguish between binary search and linear search
Binary Search Linear Search

The input list should be in It is not necessary


order
In binary search at each In linear search each element
comparison, one half of the is compared one after another
list is discarded until the element is found

The search completed much The search is slow compared


faster with binary search
Binary search requires an linear search only requires
ordering comparison equality comparisons
Binary search has complexity linear search has complexity
O(log n); O(n)
Binary search requires random linear search only requires
access to the data sequential access

Anda mungkin juga menyukai