16
Searching Arrays

Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the

Searching

Arrays

Page 2: Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the

COMP104 Lecture 22 / Slide 2

Unordered Linear Search Search an unordered array of integers for a value

and return its index if the value is found. Otherwise, return -1.

Algorithm:Start with the first array element (index 0) WHILE(more elements in array){

If value found at current index, return indexTry next element (increment index)

}Value not found, return -1

Page 3: Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the

COMP104 Lecture 22 / Slide 3

Unordered Linear Search// Searches an unordered array of integersint search(int data[], // input: array int size, // input: array size int value){ // input: value to find // output: index if found // otherwise return -

1for(int n=0; n<size; n++)

if(data[n] == value)return n;

return -1;}

Page 4: Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the

COMP104 Lecture 22 / Slide 4

Unordered Linear Search

void main() {

int A[8] = { 10, 7, 9, 1, 17, 30, 5, 6 }; int x;

cout << "Enter search element: "; cin >> x;

int index = search(A,8,x); if(index==-1)

cout << "Not found!!\n"; else

cout << "Found at: " << index << endl;

}

Page 5: Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the

COMP104 Lecture 22 / Slide 5

Ordered Linear Search

Search an ordered array of integers for a value and return its index if the value is found. Otherwise, return -1.

The key difference in design is that this array is ordered.

If we perform a linear search, and find that we have already passed where the element might be found, we can quit early.

Page 6: Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the

COMP104 Lecture 22 / Slide 6

Ordered Linear Search

not found … search for -1 search for 8 search for 100

Page 7: Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the

COMP104 Lecture 22 / Slide 7

Ordered Linear Search

Algorithm:

Start with the first array element (index 0) WHILE(more elements in the array){

If value at current index is greater than value, then value not found, return -1

If value found at current index, return indexTry next element (increment index)

}Value not found, return -1

Page 8: Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the

COMP104 Lecture 22 / Slide 8

Ordered Linear Search// Searches an ordered array of integersint lsearch(int data[], // input: array int size, // input: array size int value // input: value to find ) { // output: index if found

for(int n=0; n<size; n++){if(data[n] > value)

return -1;else if(data[n] == value)

return n;}return -1;

}

Page 9: Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the

COMP104 Lecture 22 / Slide 9

Ordered Linear Searchvoid main() {

int A[8] = { 1, 5, 6, 7, 9, 10, 17, 30 }; int x;

cout << "Enter search element: "; cin >> x;

int index = lsearch(A,8,x); if(index==-1)

cout << "Not found!!\n"; else

cout << "Found at: " << index << endl;

}

Page 10: Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the

COMP104 Lecture 22 / Slide 10

Binary Search Search an ordered array of integers for a value

and return its index if the value is found. Otherwise, return -1.

Binary search takes advantage of the sorting, to search the array efficiently.

Page 11: Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the

COMP104 Lecture 22 / Slide 11

Binary Search Binary search is based on a divide-and-

conquer strategy which works as follows: Start by looking at the middle element of the

array 1. If the middle element is smaller than the search

element (e.g. 17), eliminate the first half. 2. If the middle element is larger than the search

element (e.g. 6), eliminate the second half.

Repeat this process until the element is found, or until the entire array is eliminated.

upper boundlower bound

Page 12: Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the

COMP104 Lecture 22 / Slide 12

middle

6<10

Binary Search

2 4 6 8 10 12 14 16 18

Search for 6

lower upperupper

middle

6=6

Page 13: Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the

COMP104 Lecture 22 / Slide 13

middle

12>10

Binary Search

2 4 6 8 10 12 14 16 18

Searching for 14

lower upper

lower middle12<14

upper

middle

Page 14: Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the

COMP104 Lecture 22 / Slide 14

Binary Search Algorithm:Set lower and upper bound of array to be searched Repeat the following:

Find the middle element between lower and upper boundsIF ( middle element contains the search value )

return middle element positionELSE IF ( lower bound >= upper bound )

// nothing left to search return -1

ELSE IF ( value < the value of middle element ) set upperbound to middle element position - 1

ELSE set lower bound to middle element position + 1

Page 15: Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the

COMP104 Lecture 22 / Slide 15

Binary Search// Searches an ordered array of integersint bsearch(int data[], // input: array int size, // input: array size int value // input: value to find ) { // output: index if found // otherwise return -1

int lower, middle, upper;lower = 0;upper = size - 1;while (true) {

middle = (lower + upper) / 2; if (data[middle] == value) return middle; else if (lower >= upper) return -1; else if (value < data[middle]) upper = middle - 1; else lower = middle + 1; }}

Page 16: Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the

COMP104 Lecture 22 / Slide 16

Binary Search

void main() {int A[8] = { 1, 5, 6, 7, 9, 10, 17, 30 };int x;

cout << "Enter search element: ";cin >> x;cout << bsearch(A,8,x) << endl;int index = bsearch(A,8,x);if(index==-1)

cout << "Not found!!\n";else

cout << "Found at: " << index << endl;

}