Cha2 Algorithm
Cha2 Algorithm
Cha2 Algorithm
1
Introduction
3
Sequential /Linear search
• Basic algorithm:
1.Get the search criterion (key)
2. Get the first record from the file
3. While ( (record != key) and (still more records) )
Get the next record
End_while
4. If ( record = key )
Then success
5. Else there is no match in the file
6. exit
6
Algorithm Lsearch(List,N,key)
1. Set i=0
2. Set flag=0
3. Repeat step 4 and 5 while i<N
4. If (List[i]==key)
{
set pos=i
set flag=1
break
}
5. set i=i+1
6. If (flag==1)
print key is found at pos
else print key is not found
end if
7. exit 7
Sequential Search on an Ordered File
• Basic algorithm:
1. Get the search criterion (key)
2. Get the first record from the file
3. While ( (record < key) and (still more records) )
Get the next record
End_while
4. If ( record = key )
Then success
Else there is no match in the file
End_else
5. exit
8
Search the element 13 in the following
list using linear search.
my_array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
Contd.
my_array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
Contd.
my_array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
Contd.
my_array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
Contd.
my_array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
Contd.
my_array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
Target data found
my_array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
Example
• Search the element 39 in the following list using linear
search.
88 43 24 35 63 39 18 37 80 16
List[0] List[1] List[2] List[3] List[4] List[5] List[6] List[7] List[8] List[9]
17
Contd.
• Advantages:
- Easy algorithms to understand
- Easy to implement
- array can be in any order
Disadvantages:
- inefficient
- If there are 20,000 items in the array and what
you are looking for is in the 19,999th element,
you need to search through the entire list.
18
Binary Search
• A binary search looks for an item in a list
using a divide-and-conquer strategy
• The searching of a key element in a sorted
list will improve the efficiency
19
Binary search
The sequential search algorithm is very slow. If we have a
list of a million elements, we must do a million comparisons
in the worst case. If the list is not sorted, this is the only
solution. If the list is sorted, however, we can use a more
efficient algorithm called binary search. Generally speaking,
programmers use a binary search when a list is large.
A binary search starts by testing the data in the element at
the middle of the list. This determines whether the target is in
the first half or the second half of the list. If it is in the first
half, there is no need to further check the second half. If it is
in the second half, there is no need to further check the first
half. In other words, we eliminate half the list from further
consideration.
8.20
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
21
Figure Example of a binary search 8.22
algorithm
BinSearch(int list[ ], int n, int item, int mid){
int left=0;
int right=n-1;
int mid;
while(left<=right){
mid=(left+right)/2;
if(item> list [mid]){ left=mid+1; }
else if(item< list [mid]){right=mid-1;}
else{
item= list [mid];
index=mid;
return index; }
}// while
return false;
}
23
Binary Search
3 6 7 11 32 33 53
Binary Search
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo hi
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo mid hi
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo hi
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo mid hi
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo hi
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo mid hi
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo
hi
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo
hi
mid
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo
hi
mid
Example
• Search 20 in the following list using binary search.
12, 16, 17, 19, 20, 22, 24, 29, 30, 32, 37
Solution:
0 1 2 3 4 5 6 7 8 9 10
12 16 17 19 20 22 24 29 30 32 37
44
Contd.
Key<list[5]
Key<list[5]
46
Exercise
• Search 89in the following list using binary
search.
47
Contd.
48