Lecture 22: Arrays (cont). 2 Lecture Contents: t Searching in array: –linear search –binary...
Preview:
Citation preview
- Slide 1
- Lecture 22: Arrays (cont)
- Slide 2
- 2 Lecture Contents: t Searching in array: linear search binary
search t Multidimensional arrays t Demo programs t Exercises
- Slide 3
- 3 The problem Searching in array: to determine and return the
location of a particular value (target) Classification of searching
algorithms: t Linear search t Binary search t Search based on hash
functions
- Slide 4
- 4 43 Linear Search t The idea of a linear search is to walk
through the entire array until a target value is located t If the
target is not located, specific type of indicator needs to be
returned
- Slide 5
- 5 Linear search algorithm Problem: to search a target in array
Given:1. array (populated collection of data items) 2. target
Output:index of array element that matches the target OR -1 if
there is no match
- Slide 6
- 6 Linear search algorithm 1. Assume the target has not been
found. 2. Start with the initial array element. 3.Repeat while the
target is not found and there are more array elements { 4. If the
current element matches the target 5. Set a flag to indicate the
target was found Else 6. Advance to the next array element } // end
of repeat 7. If the target was found 8. Return the target index as
a search result Else 9. Return 1 as a search result.
- Slide 7
- 7 Linear search implemented iteratively // Iterative Linear
Search int LinSearchIterative(int target, int table[], int size) {
int k = 0; while ( k < size ) { if (target == table[k]) return
k; k++; } return -1; }
- Slide 8
- 8 Linear search implemented iteratively How to call the
function? int LinSearchIterative(int target, int table[], int
size); void main() { int tar; int m[] = { 10, 15, 8, 30, 50, 40 };
int res; tar = 20; // test other value as well res =
LinSearchIterative(tar, m, 6); if (res== -1) cout low (low the
target 9. Let high be mid1 Else 10. Let low be mid+1 } // end of
repeat 11. return 1
- Slide 16
- 16 Binary search implemented iteratively // Iterative Binary
search int BinSearchIterative(int target, int table[], int size) {
int low, high, mid; low = 0; high = size - 1; while ( low
table[mid]) low = mid + 1; } return -1; }
- Slide 17
- 17 Binary search implemented iteratively How to call the
function?
- Slide 18
- 18 Binary search implemented recursively // Recursive Binary
search int BinSearchRecursive(int target, int table[], int low, int
high) { if (low > high) return -1; int mid; mid = (low + high) /
2; if (target == table[mid]) return mid; if (target <
table[mid]) // low half of the table { // high = mid -1; return
BinSearchRecursive(target, table, low, mid -1); } if (target >
table[mid]) // high half of the table { // low = mid+1; return
BinSearchRecursive(target, table, mid +1, high); }
- Slide 19
- 19 Binary search implemented recursively How to call the
function?
- Slide 20
- 20 Binary search - Time performance Array size of n After 1
iteration array size of n/2 = n / 2^1 After 2 iterations array size
of n / 4 = n / 2^2 After x iterations array size of 1 = n / 2^x n =
2 ^ xx = ln n
- Slide 21
- 21 More on arrays Extract from Friedman/Koffman, chapter 9
- Slide 22
- Data Structures Arrays and Structs Chapter 9
- Slide 23
- 23 37 9.5 Searching and Sorting Arrays t Look at 2 common array
problems Searching Sorting t How do we go about finding the
smallest number in an array? Assume 1st is smallest and save its
position Look for one smaller If you locate one smaller save its
position
- Slide 24
- 24 38 ArrayOperations.cpp // File: arrayOperations.cpp // Finds
the subscript of the smallest value in a // subarray. // Returns
the subscript of the smallest value // in the subarray consisting
of elements // x[startindex] through x[endindex] // Returns -1 if
the subarray bounds are invalid. // Pre: The subarray is defined
and 0