Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Cha2 Algorithm

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 48

Chapter 2:

Simple Sorting and searching


Algorithm

1
Introduction

• There are some very common problems that we use computers


to solve:
– Searching through a lot of records for a specific record or set of
records
– Placing records in order, which we call sorting
• There are numerous algorithms to perform searches and sorts.
• Searching is a very fundamental activity associated in our daily
lives, whether we search for a number in the telephone
directory, an account number of an employee or a word in the
dictionary.
• Searching is basically a process of finding an element stored
either sequentially or randomly 2
Contd.
Another common algorithm in computer science is
searching, which is the process of finding the location of a
target among a list of objects.
In the case of a list, searching means that given a value, we
want to find the location of the first element in the list that
contains that value.
There are two basic searches for lists:
sequential search and binary search.
Sequential search can be used to locate an item in any list,
whereas binary search requires the list first to be sorted.

3
Sequential /Linear search

• This is the simplest method of searching, in which we compare


every element of the list with the key element we want to search.
• It uses a loop to sequentially step through an array, starting with
the first element.
• If any element of the list matches with the key element then
search is said to be successful.
• A search will be unsuccessful if all the elements of the list are
accessed and the key element does not match with any of the
elements in the list.
• Therefore, in linear search ,we start searching the key element
from the beginning of the list.
4
Figure :An example of a sequential search
5
Sequential Search on an Unordered 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
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]

• Solution: the key 39 is given


1. Set i=0;set flag =0;
2. Compare List[0] with key, 88!=39 so increase i=i+1,that is i=1
3. Compare List[1] with key, 43!=39 so increase i=i+1,that is i=2
4. Compare List[2] with key, 24!=39 so increase i=i+1,that is i=3
16
Contd.
5. Compare List[3] with key, 35!=39 so increase i=i+1,that is i=4
6. Compare List[4] with key, 63!=39 so increase i=i+1,that is i=5
7. Compare List[5] with key, 39==39 so stop searching now
and set pos=i, that is pos=5 and flag=1.
8. Since flag==1 , print key is foung at pos=5;

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

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
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.

Invariant. Algorithm maintains a[lo]  value  a[hi].

Ex. Binary search for 33.

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.

Invariant. Algorithm maintains a[lo]  value  a[hi].

Ex. Binary search for 33.

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.

Invariant. Algorithm maintains a[lo]  value  a[hi].

Ex. Binary search for 33.

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.

Invariant. Algorithm maintains a[lo]  value  a[hi].

Ex. Binary search for 33.

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.

Invariant. Algorithm maintains a[lo]  value  a[hi].

Ex. Binary search for 33.

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.

Invariant. Algorithm maintains a[lo]  value  a[hi].

Ex. Binary search for 33.

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.

Invariant. Algorithm maintains a[lo]  value  a[hi].

Ex. Binary search for 33.

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.

Invariant. Algorithm maintains a[lo]  value  a[hi].

Ex. Binary search for 33.

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.

Invariant. Algorithm maintains a[lo]  value  a[hi].

Ex. Binary search for 33.

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

1. we find that low=0, high=n-1=10,


mid=(0+10)/2=5;
2. Now, compare key(20) with List[5],that is 22. we
can see that key<List[5] so the value of high is
changed to mid-1.

44
Contd.

Key<list[5]

3. therefore, low=0, high=mid-1=4.


So mid=(0+4)/2=2
4. Now compare key(20)with list[2],that is 17.we can see
that key>list[2], so the value of low is changed to mid+1=3
45
Contd.

Key<list[5]

5. therefore, low=(mid+1)=3 and high=4, so mid=(low+high)/2=4


Key=List[mid]=20
So return List[mid]

46
Exercise
• Search 89in the following list using binary
search.

47
Contd.

48

You might also like