data structures - lecture 4 [searching in array]

11
Searching in Array Muhammad Hammad Waseem [email protected]

Upload: muhammad-hammad-waseem

Post on 15-Jul-2015

158 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Data Structures - Lecture 4 [Searching in Array]

Searching in ArrayMuhammad Hammad Waseem

[email protected]

Page 2: Data Structures - Lecture 4 [Searching in Array]

Introduction to Search Algorithms

• A search algorithm is a method of locating a specific item of information in a larger collection of data. This section discusses two algorithms for searching the contents of an array.

• Linear Search

• Binary Search

2

Page 3: Data Structures - Lecture 4 [Searching in Array]

Linear Search

• This is a very simple algorithm.

• It uses a loop to sequentially step through an array, starting with the first element.

• It compares each element with the value being searched for and stops when that value is found or the end of the array is reached.

• As soon as an equal value is found, it returns its location. If the loop finishes without finding a match, the search failed /unsuucessful and -1 is returned.

3

Page 4: Data Structures - Lecture 4 [Searching in Array]

(Linear Search)LINEAR(DATA,N,ITEM,LOC)

• Here DATA is a linear array with N elements and ITEM is a given item of the information. This algorithm finds the location LOC of ITEM in DATA, or sets the LOC:=0 if the search is unsuccessful.1. Set DATA[N+1]:=ITEM.

2. Set LOC:=1.

3. Repeat while DATA[LOC]≠ITEM.Set LOC:=LOC+1.

4. If LOC=N+1, then Set LOC:=0.

5. Exit

Page 5: Data Structures - Lecture 4 [Searching in Array]

Linear search

int linearSearch(int A[], int first, int last, int key)

{ /* function: Searches a[first]..a[last] for key.

returns: index of the matching element if it finds key, otherwise -1.

parameters: A in array of (possibly unsorted) values.

first, last in lower and upper subscript bounds

key in value to search for.

returns: index of key, or -1 if key is not in the array. */

for (int i=first; i<=last; i++)

{

if (key == A[i])

{ return i; }

}

return -1; // failed to find key

}

Page 6: Data Structures - Lecture 4 [Searching in Array]

Efficiency of the Linear Search

• The advantage is its simplicity.• It is easy to understand

• Easy to implement

• Does not require the array to be in order

• The disadvantage is its inefficiency• 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.

6

Page 7: Data Structures - Lecture 4 [Searching in Array]

7

Binary Search

• The binary search is much more efficient than the linear search.

• It requires the list to be in order.

• The algorithm starts searching with the middle element. • If the item is less than the middle element, it starts over searching the first half of the list.

• If the item is greater than the middle element, the search starts over starting with the middle element in the second half of the list.

• It then continues halving the list until the item is found.

Page 8: Data Structures - Lecture 4 [Searching in Array]

Binary Search

• The algo compares ITEM with the middle element DATA[MID] of the segment. Where MID is obtained by

MID=INT((BEG+END)/2)

• If DATA[MID] = ITEM then search is successful. Set LOC:=MID. Otherwise a new segment of DATA is obtained as follows• If ITEM<DATA[MID] then ITEM can appear only in the left half of the segment.

• DATA[BEG], DATA[BEG+1], DATA[BEG+2],…. DATA[MID-1]

• We reset END:=MID-1

• If ITEM>DATA[MID] then ITEM can appear only in the right half of the segment.• DATA[MID+1], DATA[[MID+2],…. DATA[END]

• We reset BEG:=MID+1

Page 9: Data Structures - Lecture 4 [Searching in Array]

Algorithm: BINARY(DATA,LB,UB,ITEM,LOC)

• Here DATA is a sorted array with lower bound LB and upper bound UB, and ITEM is a given item of information. The variables BEG,END, MIOD denote the beginning, end and the middle locations of a segment of elements of DATA. This algo finds the location LOC of the ITEM in DATA or sets LOC=NULL.

1. Set BEG:=LB, END:=UB and MID=INT((BEG+END)/2).2. Repeat Step 3 and 4 while BEG≤END and DATA[MID]≠ITEM.3. If ITEM<DATA[MID} then :

Set END:=MID-1.Else:

Set BEG:=MID+1.4. Set MID=INT((BEG+END)/2).5. If DATA[MID]=ITEM then:

Set LOC:=MID.Else

Set LOC:=NULL.6. EXIT

Page 10: Data Structures - Lecture 4 [Searching in Array]

Binary Searchvoid main(){

int a[10],i,n,m,c=0,LB,UB,mid;

printf("Enter the size of an array: ");

scanf("%d",&n);

printf("Enter the elements in ascending order: ");

for(i=0;i<n;i++)

{ scanf("%d",&a[i]); }

printf("Enter the number to be search: ");

scanf("%d",&m);

LB=0,UB=n-1;

while(LB<=UB){

mid=(LB+UB)/2;

if(m==a[mid])

{ c=1; break; }

else if(m<a[mid])

{ UB=mid-1; }

else

LB=mid+1;

}

if(c==0)

printf("The number is not found.");

else

printf("The number is found.");

return 0; }

Sample Output:

Enter the size of an array: 5

Enter the elements in ascending order: 4 7 8 11 21

Enter the number to be search: 11

The number is found.

Page 11: Data Structures - Lecture 4 [Searching in Array]

Efficiency of the Binary Search

• Much more efficient than the linear search.

11