Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 8 Search, Sort, Recursion

Similar presentations


Presentation on theme: "Unit 8 Search, Sort, Recursion"— Presentation transcript:

1 Unit 8 Search, Sort, Recursion
Chapters 12 and 13

2 Search

3 Number Guessing Algorithm
Activity Get in groups of 3 and have 1 person pick a number and not tell the others. The other people alternate picking numbers until they have. Questions How do you minimize the number of guesses that it takes to get the correct #?

4 Demo using a Phone Book

5 Sequential Search The simplest type of search is the sequential search. In the sequential search, each element of the array is compared to the key, in the order it appears in the array, until the desired element is found. If you are looking for an element that is near the front of the array, the sequential search will find it quickly. The more data that must be searched, the longer it will take to find the data that matches the key. index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 20 22 25 30 36 42 50 56 68 85 92 103 i How many elements will it need to examine?

6 Sequential Search Cont.
Consider this method which will search for a key integer value. If found, the index (subscript) of the first location of the key will be returned. If not found, a value of -1 will be returned. public static int seqSearch(int [ ] numbers, int key){ for (int index = 0; index < numbers.length; index++){ if ( numbers[index] == key ) return index; //We found it!!! } // If we get to the end of the loop, // a value has not yet been returned. // We did not find the key in this array. return -1;

7 Sequential Search Continued
If the key value is found, the index of the location is returned. This tells us that the return value x, will be the first integer found such that numbers[index] == key. There may be additional key locations in this array beyond this location.

8 Binary Search Do you remember playing the game "Guess a Number", where the responses to the statement "I am thinking of a number between 1 and 100" are "Too High", "Too Low", or "You Got It!"? A strategy that is often used when playing this game is to divide the intervals between the guess and the ends of the range in half. This strategy helps you to quickly narrow in on the desired number.

9 Binary Search Continued
Consider the following array of integers: We will be searching for the integer 77: Array of integers, named num, arranged in "ascending order"!! Index 1 2 3 4 5 6 7 8 9 value 13 24 34 46 52 63 77 89 91 100 Goal: Search for 77: Find the middle of the array by adding the array index of the first value to the index of the last value and dividing by two: (0 + 9) / 2 = 4 Integer division is used to arrive at the 4th index as the middle. (The actual mathematical middle would be between the 4th and 5th index, but we must work with integer indexes.) The 4th index holds the value 52, which is less than 77. Therefore, we know that 77 will be in that portion of the array to the right of 52. We now find the middle of the right portion of the array by using the same approach. (5 + 9) / 2 = 7

10 Binary Search Continued
Consider the following array of integers: We will be searching for the integer 77: Array of integers, named num, arranged in "ascending order"!! Index 1 2 3 4 5 6 7 8 9 value 13 24 34 46 52 63 77 89 91 100 Calculate the new mid, min, and max min = mid + 1 (5) max stays the same (9). mid = The (9+5 /2) = 7 Index 7 holds the integer 89, which comes after 77.

11 Binary Search Continued
Consider the following array of integers: We will be searching for the integer 77: Array of integers, named num, arranged in "ascending order"!! Index 1 2 3 4 5 6 7 8 9 value 13 24 34 46 52 63 77 89 91 100 Calculate the new mid, min, and max min stays the same (5). max = mid – 1 (6) mid = The (5+6 /2) = 5 Index 5 holds the integer 63, which comes before 77.

12 Binary Search Continued
Consider the following array of integers: We will be searching for the integer 77: Array of integers, named num, arranged in "ascending order"!! Index 1 2 3 4 5 6 7 8 9 value 13 24 34 46 52 63 77 89 91 100 Calculate the new mid, min, and max min stays the same (5). max = mid + 1 (7) mid = The (5+ 7/2) = 6 Index 6 holds the value 77.

13 Binary Search Continued.
When searching an array, the binary search process utilizes this same concept of splitting intervals in half as a means of finding the "key" value as quickly as possible. If the array that contains your data is in order (ascending or descending), you can search for the key item much more quickly by using a binary search algorithm ("divide and conquer”) How many elements will it need to examine? Example: Searching the array below for the value 42: index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 20 22 25 30 36 42 50 56 68 85 92 103 min mid max When searching an array, the binary search process utilizes this same concept of splitting intervals in half as a means of finding the "key" value as quickly as possible. If the array that contains your data is in order (ascending or descending), you can search for the key item much more quickly by using a binary search algorithm ("divide and conquer

14 Binary search code // Returns the index of an occurrence of target in a, // or a negative number if the target is not found. // Precondition: elements of a are in sorted order public static int binarySearch(int[] a, int target) { int min = 0; int max = a.length - 1; while (min <= max) { int mid = (min + max) / 2; if (a[mid] < target) { min = mid + 1; } else if (a[mid] > target) { max = mid - 1; } else { return mid; // target found } return -(min + 1); // target not found

15 Review If you have n elements in an array, what is the maximum number of array elements that might need to be accessed to find a key using the sequential search algorithm N What game is binary search like? Guessing Game In the Binary Search Algorithm, how many elements are eliminated as not being the correct key each time through the loop? Which algorithm, on average, is more efficient: sequential search or binary search? Binary search.

16 Recursion

17 Overview Why Recursion? You already know it! Definition
M. C. Escher : Drawing Hands Why Recursion? You already know it! Definition Trust the Recursion! Conclusion

18 Why recursion? Recursion can solve some kinds of problems better than iteration Iteration == loops Leads to elegant, simplistic, short code (when used well) Many programming languages ("functional" languages such as Scheme, ML, and Haskell) use recursion exclusively (no loops) A different way of thinking of problems

19 You Already Know it!

20 Recursion Recursion is an algorithmic technique where a method calls itself with some part of the task Such a method is called recursive, and is said to recur (not recurse!) when it calls itself. Many problems in computer science are particularly suited to recursive solutions We will see several of these later An equally powerful substitute for iteration (loops) This is a very tricky concept. compute()

21 Recursion Cases Every recursive algorithm involves at least 2 cases:
base case: A simple occurrence that can be answered directly. i.e If we are unstacking the Russian Nesting Dolls, have we reached the smallest nesting doll? recursive case: A more complex occurrence of the problem that cannot be directly answered, but can instead be described in terms of smaller occurrences of the same problem. Some recursive algorithms have more than one base or recursive case, but all have at least one of each. A crucial part of recursive programming is identifying these cases.

22 Trust the Recursion! When authoring recursive code:
The base case is usually easy: “When to stop?” In the recursive step How can we break the problem down into two: A piece I can handle right now The answer from a smaller piece of the problem Assume your self-call does the right thing on a smaller piece of the problem How to combine parts to get the overall answer Practice will make it easier to see idea Exercises today Examples of recursion in our discussion on sorting algorithms More practice as we lead up to the AP exam

23 Recursion The if (<problem is simple>) is called the base case
All recursive methods must have a base case What happens if they don’t? The rest of the method is called the recursive case When recurring, the problem must get “simpler” somehow What happens if it doesn’t? compute()

24 Recursion – Pseudo Java Code
The simplest recursive methods look like this: public <static> <int> method(<parameters>) { // base case if(<problem is very simple>) { <solve problem directly> } // recursive case else { <partial solution> = method(<“Smaller” params>) <solve problem based on partial solution>

25 Example 1: Line of stars Consider the following method to print a line of * characters: // Prints a line containing the given number of stars. // Precondition: n >= 0 public static void printStars(int n) { for (int i = 0; i < n; i++) { System.out.print("*"); } System.out.println(); // end the line of output Write a recursive version of this method (that calls itself). Solve the problem without using any loops. Hint: Your solution should print just one star at a time.

26 Solving for the Base Case
What are the cases to consider? What is a very easy number of stars to print without a loop? public static void printStars(int n) { if (n == 1) { // base case; just print one star System.out.println("*"); } else { ...

27 Handling more cases Handling additional cases, with no loops (in a bad way): public static void printStars(int n) { if (n == 1) { // base case; just print one star System.out.println("*"); } else if (n == 2) { System.out.print("*"); else if (n == 3) { else if (n == 4) { else ...

28 Handling more cases 2 Taking advantage of the repeated pattern (somewhat better): public static void printStars(int n) { if (n == 1) { // base case; just print one star System.out.println("*"); } else if (n == 2) { System.out.print("*"); printStars(1); // prints "*" else if (n == 3) { printStars(2); // prints "**" else if (n == 4) { printStars(3); // prints "***" else ...

29 Using recursion properly
Condensing the recursive cases into a single case: public static void printStars(int n) { // Base Case - just print one star if (n == 1) { System.out.println("*"); } // recursive case; print one more star else { System.out.print("*"); printStars(n - 1);

30 Example 2: Remembering GCD
Euclid's Algorithm Consider two positive integers, a and b. To find the Greatest Common Divisor (GCD) of these two, we follow these steps: While (b != 0), temp = b b = a MOD b a = temp The current value of a is the GCD of the original a and b values. When b = 0, then GCD is stored in a

31 Iterative GCD public static int gcd(int a, int b){ } int temp; do {
//GCD(a,b) = GCD(b, a % b) // Store b prior to assigning it, so that we can assign that value to a. temp = b; b = a % b; a = temp; } while (b != 0); return Math.abs(a); }

32 Recursive GCD public int gcd(int a, int b) { // Base Case if (b == 0){ return Math.abs(a); } // recursive case else{ return gcd( b, a % b );

33 Russian Doll Recursion Activity
In Pairs, Write two pseudo-code algorithms to determine how many dolls are nested in the Russian Dolls 1 Iterative method 1 Recursive method

34 Recursive tracing Consider the following recursive method:
public static int mystery(int n) { if (n < 10) { return n; } else { int a = n / 10; int b = n % 10; return mystery(a + b); What is the result of the following call? mystery(648)

35 A recursive trace mystery(648): int a = 648 / 10; // 64
public static int mystery(int n) { if (n < 10){ return n; } else { int a = n / 10; int b = n % 10; return mystery(a + b); A recursive trace mystery(648): int a = 648 / 10; // 64 int b = 648 % 10; // 8 return mystery(a + b); // mystery(72) mystery(72): int a = 72 / 10; // 7 int b = 72 % 10; // 2 return mystery(a + b); // mystery(9) mystery(9): return 9;

36 Recursive tracing 2 public static int mystery2(int n) {
Consider the following recursive method: public static int mystery2(int n) { if (n < 10) { return (10 * n) + n; } else { int a = mystery2(n / 10); int b = mystery2(n % 10); return (100 * a) + b; What is the result of the following call? mystery2(348)

37 A recursive trace 2 mystery2(348) int a = mystery2(34);
public static int mystery2(int n) { if (n < 10) { return (10 * n) + n; } else { int a = mystery2(n / 10); int b = mystery2(n % 10); return (100 * a) + b; A recursive trace 2 mystery2(348) int a = mystery2(34); int a = mystery2(3); return (10 * 3) + 3; // 33 int b = mystery2(4); return (10 * 4) + 4; // 44 return (100 * 33) + 44; // 3344 int b = mystery2(8); return (10 * 8) + 8; // 88 return (100 * 3344) + 88; //

38 Summary What is recursion?
Recursion is when a method calls itself in its method body Such a method is called recursive, and is said to recur (not recurse!) when it calls itself. What are the two types of cases that all recursive algorithms have? base case: A simple occurrence that can be answered directly. recursive case: A more complex occurrence of the problem that cannot be directly answered, but can instead be described in terms of smaller occurrences of the same problem.

39 Exercises Write a recursive method pow that accepts an integer base and exponent and returns the base raised to that exponent. Example: pow(3, 4) returns 81 Solve the problem recursively and without using loops. Write a recursive method fib that accepts an integer n and returns the nth number in the Fibonacci sequence. The nth Fibonacci number equals the sum of the previous two numbers if n >=3. The sequence begins with the 0th number. ….base and exponent and returns the base raised to that exponent. Example: fib(7) returns 13

40 pow solution // Returns base ^ exponent.
// Precondition: exponent >= 0 public static int pow(int base, int exponent) { if (exponent == 0) { // base case; any number to 0th power is 1 return 1; } else { // recursive case: x^y = x * x^(y-1) return base * pow(base, exponent - 1); }

41 Selection Sort The selection sort is a combination of searching and sorting. During each pass, the unsorted element with the smallest (or largest) value is moved to its proper position in the array. The number of times the sort passes through the array is one less than the number of items in the array. In the selection sort, the inner loop finds the next smallest (or largest) value and the outer loop places that value into its proper location

42 Selection sort example
Initial array: After 1st, 2nd, and 3rd passes: index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value 22 18 -4 27 30 36 50 68 91 56 85 42 98 25 index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 18 22 27 30 36 50 68 91 56 85 42 98 25 index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 22 27 30 36 50 68 91 56 18 85 42 98 25 index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 22 27 30 36 50 68 91 56 18 85 42 98 25

43 Selection Sort Continued
public static void selectionSort (int arr[] ){ int i, j, first, temp; for ( i = arr.length - 1; i > 0; i-- ){ //initialize to subscript of first element first = 0; //locate smallest element between positions 1 and i for(j = 1; j <= i; j ++){ if( arr[j] > arr[first] ){ first = j; } //swap smallest found with element in position i. temp = arr[first]; arr[first] = arr[i]; arr[i] = temp;

44 Selection Sort Summary
Selection sort is an in place comparison sort Selection sort has the property of minimizing the number of swaps. In applications where the cost of swapping items is high, selection sort very well may be the algorithm of choice. It is much less efficient on large lists than the more advanced algorithms such as quicksort, heapsort, or merge sort The algorithm finds the minimum value, swaps it with the value in the first position, and repeats these steps for the remainder of the list. It does no more than n swaps, and thus is useful where swapping is very expensive.

45 Insertion Sort Insertion sort algorithm somewhat resembles selection sort. Array is imaginary divided into two parts - sorted one and unsorted one. At the beginning, sorted part contains first element of the array and unsorted one contains the rest. At every step, algorithm takes first element in the unsorted part and inserts it to the right place of the sorted one. When unsorted part becomes empty, algorithm stops.

46 Insertion Sort Continued
Insertion sort algorithm step looks like this: becomes

47 Insertion sort example
Initial array: After 1st, 2nd, 3rd and 8th passes: index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value 22 18 -4 27 30 36 50 68 91 56 85 42 98 25 index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value 18 22 -4 27 30 36 50 68 91 56 85 42 98 25 index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value 18 22 -4 27 30 36 50 68 91 56 85 42 98 25 index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 18 22 27 30 36 50 68 91 56 85 42 98 25 index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 18 22 27 30 36 50 68 91 56 85 42 98 25

48 Insertion Sort Continued
The main operation of the algorithm is insertion. The task is to insert a value into the sorted part of the array. Let us see the variants of how we can do it. "Sifting down" using swaps The simplest way to insert next element into the sorted part is to sift it down, until it occupies correct position. Initially the element stays right after the sorted part. At each step algorithm compares the element with one before it and, if they stay in reversed order, swap them.

49 Insertion Sort Continued
This approach writes sifted element to temporary position many times. Next implementation eliminates those unnecessary writes.

50 Insertion Sort Continued
Shifting instead of swapping We can modify previous algorithm, so it will write shifted element only to the final correct position. Let us see an illustration.

51 Insertion Sort Method public static void insertionSort(int[] arr) { int i, j, newValue; for (i = 1; i < arr.length; i++) { // Get a new value to insert into our "sorted" array newValue = arr[i]; j = i; // Shift values to right as long as they are greater // than new value while (j > 0 && arr[j - 1] > newValue) { arr[j] = arr[j - 1]; j--; } // Place our new value in it's correct position arr[j] = newValue;

52 Insertion Sort Summary
Insertion sort is a simple sorting algorithm, a comparison sort in which the sorted array (or list) is built one entry at a time. It is much less efficient on large lists than the more advanced algorithms such as quicksort, heapsort, or merge sort, but it has various advantages: Simple to implement Efficient on (quite) small data sets Efficient on data sets which are already substantially sorted Stable (does not change the relative order of elements with equal keys) In abstract terms, each iteration of an insertion sort removes an element from the input data, inserting it at the correct position in the already sorted list, until no elements are left in the input. The choice of which element to remove from the input is arbitrary and can be made using almost any choice algorithm.

53 Sorting Algoritms Visual

54 Summary How would you describe the Selection Sort algorithm – that is what happens in each pass? The algorithm finds the minimum value, swaps it with the value in the first position, and repeats these steps for the remainder of the list. When is Selection Sort – the sorting algorithm of choice? Selection sort has the property of minimizing the number of swaps. In applications where the cost of swapping items is high, selection sort very well may be the algorithm of choice. How would you describe the Insertion Sort algorithm – that is what happens in each pass? Each iteration of an insertion sort removes an element from the input data, inserting it at the correct position in the already sorted list, until no elements are left in the input. When is Insertion Sort – the sorting algorithm of choice? Insertion sort is the algorithm of choice either when the data is nearly sorted (because it is adaptive) or when the problem size is small (because it has low overhead).

55 Mergesort

56 When to use Mergesort Stability is required – more on that in a minute. Sorting linked lists – a data structure that we haven’t talked about. When random access is much more expensive than sequential access For example, external sorting on tape. The Arrays class has a sort method that is overloaded. For numeric types, Quicksort is used. For objects, Mergesort is used.

57 Stability – what is it? Stability means that equivalent elements retain their relative positions, after sorting. Suppose you want to sort the following list based upon the state: There are 2 correct outputs The 1st one is stable, because the State key is sorted and the city field maintains the order of the original list. City State New York NY Chicago IL Renton WA Issaquah Los Angeles CA City State Los Angeles CA Chicago IL New York NY Renton WA Issaquah City State Los Angeles CA Chicago IL New York NY Issaquah WA Renton

58 Stability – Why is it important?
For primitives – is really isn’t For objects – it definitely is. Example What do you expect to happen if you sort messages first by date and then by sender? You expect them to be sorted by date within each sender That only happens when your sort algorithm is stable.

59 Merge sort merge sort: Repeatedly divides the data in half, sorts each half, and combines the sorted halves into a sorted whole. The algorithm: Divide the list into two roughly equal halves. Sort the left half. Sort the right half. Merge the two sorted halves into one sorted list. Often implemented recursively. An example of a "divide and conquer" algorithm. Invented by John von Neumann in 1945

60 Merge sort example index 1 2 3 4 5 6 7 value 22 18 12 -4 58 31 42 22
1 2 3 4 5 6 7 value 22 18 12 -4 58 31 42 split 22 18 12 -4 58 7 31 42 split split 22 18 12 -4 58 7 31 42 split split split split 22 18 12 -4 58 7 31 42 merge merge merge merge 18 22 -4 12 7 58 31 42 merge merge -4 12 18 22 7 31 42 58 merge -4 7 12 18 22 31 42 58

61 Merging sorted halves

62 Merge halves code // Merges the left/right elements into a sorted result. // Precondition: left/right are sorted public static void merge(int[] result, int[] left, int[] right) { int i1 = 0; // index into left array int i2 = 0; // index into right array for (int i = 0; i < result.length; i++) { if (i2 >= right.length || (i1 < left.length && left[i1] <= right[i2])) { result[i] = left[i1]; // take from left i1++; } else { result[i] = right[i2]; // take from right i2++; }

63 Merge sort code // Rearranges the elements of a into sorted order using // the merge sort algorithm (recursive). public static void mergeSort(int[] a) { if (a.length >= 2) { // split array into two halves int[] left = Arrays.copyOfRange(a, 0, a.length/2); int[] right = Arrays.copyOfRange(a, a.length/2, a.length); // sort the two halves // merge the sorted halves into a sorted whole merge(a, left, right); }

64 Merge sort code 2 // Rearranges the elements of a into sorted order using // the merge sort algorithm (recursive). public static void mergeSort(int[] a) { if (a.length >= 2) { // split array into two halves int[] left = Arrays.copyOfRange(a, 0, a.length/2); int[] right = Arrays.copyOfRange(a, a.length/2, a.length); // sort the two halves mergeSort(left); mergeSort(right); // merge the sorted halves into a sorted whole merge(a, left, right); }

65 Summary When is Mergedort the sorting algorithm of choice?
Stability is required What are the 3 main steps of Mergesort? Divide the list into two roughly equal halves. Sort the halves. Merge the two sorted halves into one sorted list.

66 The compareTo method (10.2)
The standard way for a Java class to define a comparison function for its objects is to define a compareTo method. Example: in the String class, there is a method: public int compareTo(String other) A call of A.compareTo(B) will return: a value < 0 if A comes "before" B in the ordering, a value > 0 if A comes "after" B in the ordering, or 0 if A and B are considered "equal" in the ordering.

67 Unit Review

68 Unit Review Sort (Barrons 2q30) Recursion (cb q16)
Consider the following mergeSort method and the private instance variable a both in the same Sorter class: private Comparable [] a; /* sorts a[first] to a[last] in increasing order. */ Public void mergeSort (int first, int last) { if (first != last) int mid = (first + last) /2; mergeSort(first, mid); mergeSort(mid+1, last); merge(first, mid, last); } Method mergeSort calls method merge, which has this header: // Merge a[lb] to a[mi+1] to a[ub]. Private void merge (int lb, int m, int ub) If the first call to mergeSort is mergeSort(0,3) how many further calls will there be to mergeSort before an array b[0]…b[3] is sorted? 2 3 4 5 6 Recursion (cb q16) Consider the following recursive method. public static int mystery(int n) { if (n == 0) return 1; else return 3 * mystery(n - 1); } What value is returned as a result of the call mystery(5) ? (a) 0 (b) 3 (c) 81 (d) 243 (e) 6561 Search (Barrons 3q30) Under which conditions will a sequential search of arr be faster than a binary search? Consider a sorted array arr of n elements, where n is large and n is even. I The target is not in the list II The target is in the first position of the list III the target is in arr[1 + n/2] I only II only III only I and III only II and III only 1.D 2.E 3.B

69 CollegeBoard

70 Videos Search & Sort


Download ppt "Unit 8 Search, Sort, Recursion"

Similar presentations


Ads by Google