Presentation is loading. Please wait.

Presentation is loading. Please wait.

June 15 16 Searching CE00858-1: Fundamental Programming Techniques 16 Searching1.

Similar presentations


Presentation on theme: "June 15 16 Searching CE00858-1: Fundamental Programming Techniques 16 Searching1."— Presentation transcript:

1 June 15 16 Searching CE00858-1: Fundamental Programming Techniques 16 Searching1

2 June 15 Objectives In this session, we will: implement methods to search arrays 16 Searching2

3 June 15 Linear search of unsorted array method to search an unsorted array for a specific item and return location return -1 if not present examples: search for 8: return location 2 search for 13: return -1 search each item in turn until it is found or the end of the array is reached 7 0123456 2082191514 16 Searching3

4 4June 15 Analysis of linear search of unsorted array what data is used? arr: integer array of unsorted items to be searched item: integer to be located what operations are performed? iteration needed to search array locations how many times is loop repeated? loop repeated until item found or end of array reached what operations are done once before the loop? initialise location to 0 what operations are repeated inside the loop? increment location what operations are done once after the loop? set location to -1 if item not found return location June 15 16 Searching 4

5 5June 15 Linear search of unsorted array code public static int linearUnsorted(int[] arr, int item) { int location = 0; while (location < arr.length && arr[location] != item) { location++; } if (location == arr.length) { location = -1; } return location; } June 15 16 Searching 5 linearUnsorted.java

6 6June 15 Linear search of unsorted array testing test data must test: item is first element in array item is last element in array item not present June 15 16 Searching 6

7 June 15 Linear search of sorted array method to search a sorted array for a specific item return -1 if not present examples: search for 8: return location 3 search for 13: return -1 search for 17: return -1 search each item in turn until it is found, the end of the array is reached or the item can’t be there 2 0123456 478121415 16 Searching7

8 8June 15 Analysis of linear search of sorted array what data is used? arr: integer array of sorted items to be searched item: integer to be located what operations are performed? iteration needed to search array locations how many times is loop repeated? loop repeated until item found, end of array reached or item larger than value in array what operations are performed before the loop? initialise location to 0 what operations are repeated inside the loop? increment location what operations are performed after the loop? set location to -1 if item not found return location June 15 16 Searching 8

9 9June 15 Linear search of sorted array code public static int linearSorted(int[] arr, int item) { int location = 0; while (location < arr.length && arr[location] != item && arr[location] < item) { location ++; } if (location == arr.length || arr[location] > item) { location = -1; } return location; } June 15 16 Searching 9 linearSorted.java

10 10June 15 Linear search of sorted array testing test data must test: item is first element in array item is last element in array item before first element in array item after last element in array June 15 16 Searching 10

11 June 15 Binary search of sorted array method to search a sorted array for a specific item return -1 if not present examples: search for 4: return location 1 search for 10: return -1 check portions of the array by dividing it in half each time and checking in the top or bottom portion as appropriate 2 0123456 478121415 16 Searching11

12 12June 15 Analysis of binary search of sorted array what data is used? arr: integer array of sorted items to be searched item: integer to be located what operations are performed? iteration needed to search array locations how many times is loop repeated? loop repeated until item found or item can’t be in the array what operations are performed before the loop? initialise low to 0 initialise high to array length – 1 initialise location to (low + high) / 2 what operations are repeated inside the loop? if item not in top portion, set high to location if item not in bottom portion, set low to location + 1 set location to (low + high) / 2 what operations are performed after the loop? set location to -1 if item not found return location June 15 16 Searching 12

13 13June 15 Binary search of sorted array: code public static int binarySearch(int[] arr, int item) { int low = 0; int high = arr.length - 1; int location = (low + high) / 2; while (item != arr[location] && high >= low) { if (item < arr[location]) { high = location - 1; } else if (item > arr[location]) { low = location + 1; } location = (low + high) / 2; } if (item != arr[location]) { location = -1; } return location; } June 15 16 Searching 13 Binary.java

14 14June 15 Binary search of sorted array testing test data must test: item is first element in array item is last element in array item before first element in array item after last element in array June 15 16 Searching 14

15 June 15 Summary In this session we have: introduced 3 ways of searching data In the next session we will: introduce exception handling to deal with unexpected situations 16 Searching15


Download ppt "June 15 16 Searching CE00858-1: Fundamental Programming Techniques 16 Searching1."

Similar presentations


Ads by Google