capter 6 searching algorithm. what is searching process of finding an item in an array two ways to...

23
CAPTER 6 SEARCHING ALGORITHM

Upload: cleopatra-miles

Post on 19-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

CAPTER 6

SEARCHING ALGORITHM

Page 2: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

WHAT IS SEARCHING

Process of finding an item in an array

Two ways to find an item

By position / By value

Page 3: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

Application

Phone Number in List

Word in Dictionary

Accessing Website

Page 4: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

LINEAR SEARCH

Page 5: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

DEFINITION

Sequential Search

Look for successive elements

Stops when found the value

Stops when no value has been found

Good for small arrays

Page 6: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

Ad/Disad-Vantages

Advantages• Simple• Does not necessarily need

to be in order• Good for item located in

front list

Disadvantages• Not so efficient• Looking at beginning of the

list only• Only efficient if the data is

sorted

Page 7: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

Algorithm in General

For all the items in the listCompare the item with the desired itemIf the item was found

Return the index value of the current itemEndif

EndForReturn -1 because the item was not found

Page 8: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

Code in C++#include<iostream>using namespace std;const int NUMEL=10;int linearSearch(int[], int, int);

int main(){

int nums[NUMEL]={5,10,22,32,45,67,73,98,99,101};int item,location;

cout<<"Enter the item you are searching for: ";cin>>item;

location=linearSearch(nums,NUMEL,item);if(location> -1)

cout<<"The item was found at index location "<<location<<endl;else

cout<<"The item was not found in the array\n";return 0;

}

Page 9: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

Code in C++ (Cont.)

//this function returns the location of key in the list//a -1 is returned if the value is not found

int linearSearch(int list[ ],int size,int key) {int i;

for(i=0;i<size;i++) {if (list[i]==key)return i;

} return -1;}

Page 10: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

ExampleDefault 5 10 22 32 45 67 73 98 99 101

Try to

find “32”

• “5” with “32” = NO• “10” with “32” = NO• “22” with “32” = NO• “32” with “32” = YES

Page 11: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

BINARY SEARCH

Page 12: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

DEFINITION

Efficient if sorted

All must be sorted first

Divide the array into half

Page 13: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

Process of Binary Search

1st. Look at the middle element

2nd. If equal, then finish

3rd. If less, do left binary searching

4th. If greater, do right binary searching

Page 14: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

Algorithm Set the lower index to 0

Set the upper index to one less than the size of the list

Begin with the first item in the list

While the lower index is less than or equal to the upper index

Set the midpoint index to the integer average of the lower and upper index values

Compare the desired item to the midpoint element

If the desired element equals the midpoint element

Return the index value of the current item

Else if the desired element is greater than the midpoint element

Set the lower index value to the midpoint value plus 1

Else if the desired element is less than the midpoint element

Set the upper index value to the midpoint value minus1

Endif

EndWhile

Return -1 because the item was not found

Page 15: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

Code in C++#include<iostream>using namespace std;

const int NUMEL=10;int binarySearch(int[], int, int);

int main(){

int nums[NUMEL]={5,10,22,32,45,67,73,98,99,101};int item,location;

cout<<"Enter the item you are searching for: ";cin>>item;location=binarySearch(nums,NUMEL,item);if(location> -1)

cout<<"The item was found at index location "<<location<<endl;else

cout<<"The item was not found in the array\n";return 0;

}

Page 16: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

Code in C++//this function returns the location of key in the list//a -1 is returned if the value is not foundint binarySearch(int list[],int size,int key){

int left,right,midpt;left=0;//set the lower index to 0right=size-1;//set the upper index to one less than the size of the list

while(left<=right) {midpt=(int)((left+right)/2);//set the midpoint indexif(key==list[midpt])//compare the desired item to the midpoint element{return midpt;}else if(key>list[midpt])left=midpt+1;//set the lower index value to midpoint value plus 1elseright=midpt-1;//set the upper index value to midpoint value minus 1}return -1;//return -1 if the item was not found

}

Page 17: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

Example Binary 1Default 5 10 22 32 45 67 73 98 99 101

1st pass 5 10 22 32 45

2nd pass 10

Find item “10”

• 1st pass : Left = 0, Right = 9, Mid = 4, List[4]=45. Thus Key=10<List[4]. Right=3

• 2nd pass : Left = 0, Right = 3, Mid = 1, List[1]=10. Thus Key=10==List[1]. Done

Page 18: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

Example Binary 2

Find item “32”

• 1st pass : Left = 0, Right = 9, Mid = 4, List[4]=45. Thus Key=32<List[4]. Right=3

Default 5 10 22 32 45 67 73 98 99 101

1st pass 5 10 22 32 45

2nd pass 22 32 45

3rd pass 32 45

4th pass 32

Page 19: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

Example Binary 2(Cont.)

Find item “32”

• 2nd pass : Left = 0, Right = 3, Mid = 1, List[1]=10. Thus Key=32>List[1]. Left = 2

Default 5 10 22 32 45 67 73 98 99 101

1st pass 5 10 22 32 45

2nd pass 22 32 45

3rd pass 32 45

4th pass 32

Page 20: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

Example Binary 2(Cont.)

Find item “32”

• 3rd pass : Left = 2, Right = 3, Mid = 2, List[2]=22.Thus Key=32>List[2]. Left = 3

Default 5 10 22 32 45 67 73 98 99 101

1st pass 5 10 22 32 45

2nd pass 22 32 45

3rd pass 32 45

4th pass 32

Page 21: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

Example Binary 2(Cont.)

Find item “32”

• 4th pass : Left = 3, Right =3, Mid = 3, List[3]=32. Thus Key=32==List[3]. Done

Default 5 10 22 32 45 67 73 98 99 101

1st pass 5 10 22 32 45

2nd pass 22 32 45

3rd pass 32 45

4th pass 32

Page 22: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value

COMPARISON LINEAR/BINARY

LINEAR SEARCH• O(n)• Best for unsorted array

(sequential access)• Needs more space and time

complexity. • Not suitable for large set of

data

BINARY SEARCH• O(nlog2n)• Best for sorted array (liner

access)• Best practice in application

since most data is sorted in real world application

• Suitable for large set of data

Page 23: CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value