COMP 102 Programming Fundamentals I - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

COMP 102 Programming Fundamentals I

Description:

The process used to find the location of a target among a list ... return numb * fac(numb-1); COMP 102 Lab 12. 14. Recursion. Advantage. For certain problems ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 16
Provided by: typey73
Category:

less

Transcript and Presenter's Notes

Title: COMP 102 Programming Fundamentals I


1
COMP 102Programming 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

3
Example
4
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

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

6
1. Unordered Linear Search
  • E.g.

// output // if found gt index // otherwise
gt return -1 int search(int datasize, int size,
int value) for (int index0 indexltsize
index) if (dataindex value)
cout ltlt "Value found at index "
ltlt index ltlt endl return index
return -1
7
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

8
2. Ordered Linear Search
  • E.g.

int lsearch(int data, int size, int value)
for(int index0 indexltsize index) if
(dataindex gt value) return -1
else if (dataindex value) return
index return -1
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

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 gt last )
  • return 1
  • else if (value lt the value of middle element)
  • set last to middle element position 1
  • else
  • set first to middle element position 1

11
3. Binary Search
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
(datamiddle value) return middle
else if (first gt last) return
-1 else if (value lt datamiddle)
last middle - 1 else
first middle 1
  • E.g.

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 itself
  • E.g.
  • Factorial function
  • 6! 6 5 4 3 2 1
  • With recursion
  • 6! 6 5!

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 definition

int fac(int numb) if (numb lt 1) return
1 else return numb fac(numb-1)
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 counter
  • Note
  • Must be careful not to create an infinite loop by
    accident
  • Contain termination condition

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
Write a Comment
User Comments (0)
About PowerShow.com