Title: COMP 102 Programming Fundamentals I
1COMP 102Programming Fundamentals I
- Presented by Timture Choi
2Searching
- 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
3Example
41. 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
51. 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
61. Unordered Linear Search
// 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
72. 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
82. Ordered Linear Search
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
93. 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
103. 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
113. 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
12Recursion
- 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!
13Recursion
- 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)
14Recursion
- 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
15SUMMARY
- 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