Presentation is loading. Please wait.

Presentation is loading. Please wait.

L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: 2 13 5 19 10 7 27 17 1 int a[8] 5191072717113.

Similar presentations


Presentation on theme: "L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: 2 13 5 19 10 7 27 17 1 int a[8] 5191072717113."— Presentation transcript:

1 L. Grewe

2  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: 2 13 5 19 10 7 27 17 1 int a[8] 5191072717113

3  Problem: determine if an element is present in an array  Method: ◦ start at one end ◦ look at each array element until the sought element is found  Also called sequential search 3

4 isPresent (array, val, arraySize) { set count to 0 for (each element in the array) { if ( this array element is val ) { return true } increment count } return false } 4 int isPresent (int *arr, int val, int N) { int count; for (count=0;count<N;count++) { if (arr[count]==val) { return 1; } return 0; }

5  How would you modify the program so that it returns the position of the sought item (i.e., findPosition rather than isPresent )?  How would you indicate “not found”? 5

6  Algorithm: a set of instructions describing how to do a task  Program: an implementation of an algorithm 6 The time and space requirements of an algorithm enable us to measure how efficient it is

7  Time: elapsed period from start to finish of the execution of an algorithm  Space (memory): amount of storage required by an algorithm  Hardware: physical mechanisms required for executing an algorithm 7

8  Use your watch? Use the computer clock?  Not a good idea, because: ◦ What if you run your program on different computers? ◦ Your program may also wait for I/O or other resources ◦ While running a program, a computer performs many other computations ◦ Depends on programming/coding skill 8

9  We are interested in the number of steps executed by an algorithm ◦ step  execution of an instruction  The running time of an algorithm is proportional to the number of steps it takes to execute the algorithm 9

10  What is the size of the input data? ◦ The size of the array being searched is N  How efficient is this algorithm? ◦ Each time through the loop we perform  2 comparisons  count<N  arr[count]==val  1 increment and 1 assignment  count++  Total: 4 operations 10

11  Best case? ◦ Wanted item is at the start of the list  1 (initialization) + 4 operations  Worst case? ◦ Wanted item is not found  1 + 4N + 2 (last increment and test)  Average case? ◦ Average of [Wanted item is in position 1, 2, …, N ] 11

12  Can we do any better than linear search?  Example: ◦ How do you find a word in the dictionary, or a number in the phone directory? ◦ Choose a spot, then work out whether you're before or after the entry you're looking for ◦ Assume that the array is sorted and use bisection 12

13 13 If ( value == middle element ) value is found else if ( value < middle element ) search left-half of list with the same method else search right-half of list with the same method

14 14 Case 1: val == a[mid] val = 10 low = 0, high = 8 57910131719127 123456708 a: lowhigh Binary Search -- Example 1 mid mid = (0 + 8) / 2 = 4 10

15 15 Case 2: val > a[mid] val = 19 low = 0, high = 8 mid = (0 + 8) / 2 = 4 Binary Search -- Example 2 579101 13171927 123456708 a: mid lowhigh new low new low = mid + 1 = 5 13171927

16 16 Case 3: val < a[mid] val = 7 low = 0, high = 8 mid = (0 + 8) / 2 = 4 Binary Search -- Example 3 10131719 5791 27 123456708 a: mid lowhigh new high new high = mid - 1 = 3 5791

17 17 val = 7 low = 0, new high = 3 new mid = (0 + 3) / 2 = 1 val>a[new mid] Binary Search -- Example 3 (cont) 5 79 10131719127 123456708 a: lownew high new mid new low new low = mid + 1 = 2 79

18 18 7 newest mid newest mid = (2 + 3) / 2 = 2 7 val = 7 new low = 2, new high = 3 Binary Search -- Example 3 (cont) 5910131719127 123456708 a: new low new high

19 set low to lower bound of array set high to upper bound of array while ( low ≤ high ) { set mid to half of low + high if (array element in mid is val ) { finish - val is in array } else if ( middle value < val ) { set low to mid + 1 } else { set high to mid - 1 } } finish - val is not in array 19

20  What happens if the sought value is not in the list?  How would you modify the code so that it returns the position of the sought item (i.e., findPosition rather than isPresent )? 20

21  Linear search can be done on any (sorted or unsorted) list, but it is inefficient  Binary search ◦ requires a list to be sorted ◦ is more efficient  Sorting a list: A future lecture 21


Download ppt "L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: 2 13 5 19 10 7 27 17 1 int a[8] 5191072717113."

Similar presentations


Ads by Google