15
COMP102 Lab 12 1 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi

Embed Size (px)

Citation preview

Page 1: COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 12 1

COMP 102

Programming Fundamentals I

Presented by : Timture Choi

Page 2: COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi

2

Searching

The process used to find the location of a target among a list Searching an array finds the index of first

element in an array containing that value

Page 3: COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 12 3

Example

Page 4: COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 12 4

1. Unordered Linear Search

Search an unordered array of integers for a value If the value is found

Return its index Otherwise

Return -1Unordered array

14 2 10 5 1 3 17 2

Page 5: COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 12 5

1. Unordered Linear Search

Algorithm Start with the first array element (index = 0)

while (more elements in array) { if value found at current index

return index; else {

Try next element by incrementing index

} } return -1; //if value not found

Page 6: COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 12 6

1. Unordered Linear Search

E.g.// output:// if found => index// otherwise => return -1int search(int data[size], int size, int value) { for (int index=0; index<size; index++) { if (data[index] == value) { cout << "Value found at index " << index << endl; return index; } } return -1;}

Page 7: COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 12 7

2. Ordered Linear Search

Ordered arrayAlgorithm

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

if value at current index is greater than value value not found return –1;

if value found at current index return index;

Try next element by incrementing index } return –1; //if value not found

Advantage Linear search can stop immediately

when it has passed the possible position of the search value

1 2 3 5 7 10 14 17

Page 8: COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 12 8

2. Ordered Linear Search

E.g.

int lsearch(int data[], int size, int value) { for(int index=0; index<size; index++) { if (data[index] > value) return -1; else if (data[index] == value) return index; } return -1;}

Page 9: COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 12 9

3. Binary Search

Start by looking at the middle element of an ordered array

If the value it holds is lower than the search element Eliminate the first half of the array from further

consideration If the value it holds is higher than the search

element Eliminate the second half of the array from further

consideration

Repeat this process Until the element is found OR until the entire array has been eliminated

Advantage Binary search skips over parts of the array

Page 10: COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 12 10

3. Binary Search

Algorithm Set first and last boundary of array to be

searched Repeat the following

Find middle element between first and last boundaries;

if (middle element contains the search value) return middle element position;

else if (first >= last ) return –1;

else if (value < the value of middle element) set last to middle element position – 1;

else set first to middle element position + 1;

Page 11: COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 12 11

3. Binary Search

E.g.int bsearch(int data[], int size, int value) { int first, middle, last; // indexes to the array first = 0; last = size - 1;

while (true) { middle = (first + last) / 2; if (data[middle] == value) return middle; else if (first >= last){ return -1; } else if (value < data[middle]) last = middle - 1; else first = middle + 1; }}

Page 12: COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 12 12

Recursion

Typically, a functions can call other functions to do part of the work But functions can also call themselves Recursion

In some problems It may be natural to define the problem in terms

of the problem itselfE.g. Factorial function

6! = 6 * 5 * 4 * 3 * 2 * 1 With recursion

6! = 6 * 5!

Page 13: COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 12 13

Recursion

In general if n is larger than 1

n! = n * (n-1)! // recursive formula if n is equal to 1

n! = 1 // termination condition

The C++ equivalent of this definitionint fac(int numb){ if (numb <= 1) return 1; else return numb * fac(numb-1);}

Page 14: COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 12 14

Recursion

Advantage For certain problems

E.g. the factorial function Leads to short and elegant code

Disadvantage Calling a function consumes more time and

memory than adjusting a loop counterNote Must be careful not to create an infinite loop by

accident Contain termination condition

Page 15: COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 12 15

SUMMARY

By the end of this lab, you should be able to: Search an element from an array with

Unordered linear search Ordered linear search Binary search

Write recursive function