malik-higgins
Uploaded by
11 SLIDES
248 VUES
110LIKES

Understanding Recursion in Programming: Key Concepts and Examples

DESCRIPTION

This guide provides an overview of recursion, a pivotal programming methodology. Every recursive method consists of three essential components: a test to stop or continue the recursion, a base case that terminates the recursion, and recursive calls to proceed with the recursion. Through practical examples, we illustrate recursive methods such as calculating factorials, summing positive integers, and generating string anagrams. Additionally, we explore the quicksort algorithm, highlighting its recursive nature and partitioning process. This resource is ideal for those looking to deepen their understanding of recursion in programming.

1 / 11

Télécharger la présentation

Understanding Recursion in Programming: Key Concepts and Examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1101: Programming MethodologyRecitation 9 – Recursion

  2. Recursive Methods • Every recursive method has the following elements: • A test to stop or continue the recursion. • An end case (base case) that terminate the recursion. • A recursive call(s) that continues the recursion. • Example factorial method public int factorial (int N) { if (N == 1) { return 1; } else { return N * factoral (N-1); } } Test to stop or continue End case: recursion stops Recursive case: recursion continues with recursive call

  3. Recursion for Mathematical Functions • Compute the sum of first N positive integers public int sum (int N) { if (N == 1) { return 1; } else { return N + sum (N-1); } } • Compute the exponential AN public double exponent (double A, int N) { if (N == 1) { return A; } else { return A * exponent (A, N-1); } } Pass two arguments: Value of A does not change in the calls, but the value of N is decremented after each recursive call

  4. Recursion for Nonnumerical Applications • Compute the length of a string public int length (String str) { if (str.equals(“”)) { //str has no characters return 0; } else { return 1 + length (str.substring(1)); } } • Compute all the anagrams of a word. • An anagram is a word formed by reordering letters of another word • Example: anagrams of CAT are CTA, ATC, ACT, TCA, TAC Index of second position is 1

  5. rotate left rotate left rotate left H L O A L A O H O H A L L H A O Anagram recursion Apply recursion to find all the anagrams of these three letters recursion recursion recursion

  6. Recursive algorithm to find anagrams public void anagram (String word) { int numOfChars = word.length(); if (numOfChars == 1) { //end case – cannot recurse anymore } else { for (int i=1; i <= numOfChars; i++) { char firstLetter = word.charAt(0); suffix = word.substring (1, numOfChars); anagram (suffix); //recurse with remaining letters in word //rotate left word = suffix + firstLetter; } } } • What do we do when recursion stops ? • Print out the anagram found • But words passed in successive recursive calls are getting shorter • since we chop off the first letter • Pass two parameters – prefix and suffix

  7. Recursive algorithm to find anagrams public void anagram (String prefix, String suffix) { int numOfChars = suffix.length(); if (numOfChars == 1) { //end case – print out one anagram System.out.println (prefix + suffix); } else { for (int i=1; i <= numOfChars; i++) { String newSuffix = suffix.substring (1, numOfChars); String newPrefix = prefix + suffix.charAt(0); anagram (newPrefix, newSuffix); //recursive call //rotate left to create a new rearranged suffix suffix = newSuffix + suffix.charAt(0); } } } • Call method initially with empty prefix and word as suffix • Anagram (“”, “HALO”); • What happens when user enter an empty string ? • Anagram (“”, “”);

  8. Quicksort low high … Array number p partition number[i] < p number[i] > p p Quicksort Quicksort mid

  9. Quicksort • Any element can be used as a pivot. • For simplicity, use number[low] as pivot p. • Scan array and move elements smaller than p to left half and • elements larger than p to upper half. • 4. Sort lower and upper halves recursively, using quicksort. • 5. Pivot p is placed at location mid. • 6. Recursion stops when low >= high.

  10. public void quickSort (int[] number, int low, int high) { if (low < high) { int mid = partition (number, low, high); quickSort (number, low, mid-1); quickSort (number, mid+1, high ); } } private int partition (int[] number, int start, int end) { int pivot = number[start]; //start the pivot do { //look for a number smaller than pivot from the end while (start < end && number[end] >= pivot) { end--; } if (start < end) { //found a smaller number number[start] = number[end]; //now find a number larger than pivot from the start while (start < end && number[start] <= pivot) { start++; } if (start < end) { //found a larger number number[end] = number[start]; } } } while (start < end); number[start] = pivot; //done. Move pivot back to array return start; }

  11. 23 12 12 12 12 23 12 17 17 17 17 17 17 17 5 5 5 5 5 5 5 90 90 23 90 90 90 90 12 12 90 12 90 12 90 48 48 48 48 48 48 48 37 37 37 37 37 37 37 83 83 83 83 83 83 83 77 77 77 77 77 77 77 mid start end pivot 0 1 2 3 4 5 6 7 8 23 pivot = number[start] end start 0 1 2 3 4 5 6 7 8 while (number[end] > pivot) end--; number[start] = number[end] end start 0 1 2 3 4 5 6 7 8 while (number[start] < pivot) start++; number[end] = number[start] start, end 0 1 2 3 4 5 6 7 8 while (number[end] > pivot) end--; number[start] = pivot

More Related
SlideServe
Audio
Live Player
Audio Wave
Play slide audio to activate visualizer