Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi."— Presentation transcript:

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

2 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

3 COMP102 Lab 123 Example

4 COMP102 Lab 124 1. Unordered Linear Search Search an unordered array of integers for a value If the value is found  Return its index Otherwise  Return -1 Unordered array 14 2 10 5 1 3 17 2

5 COMP102 Lab 125 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

6 COMP102 Lab 126 1. Unordered Linear Search E.g. // output: // if found => index // otherwise => return -1 int 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; }

7 COMP102 Lab 127 2. Ordered Linear Search Ordered array Algorithm 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

8 COMP102 Lab 128 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; }

9 COMP102 Lab 129 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

10 COMP102 Lab 1210 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;

11 COMP102 Lab 1211 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; }

12 COMP102 Lab 1212 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 itself E.g. Factorial function  6! = 6 * 5 * 4 * 3 * 2 * 1 With recursion  6! = 6 * 5!

13 COMP102 Lab 1213 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 definition int fac(int numb){ if (numb <= 1) return 1; else return numb * fac(numb-1); }

14 COMP102 Lab 1214 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 counter Note Must be careful not to create an infinite loop by accident  Contain termination condition

15 COMP102 Lab 1215 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


Download ppt "COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi."

Similar presentations


Ads by Google