1 / 73

Recursion & Sort Algorithms

Recursion & Sort Algorithms. Recursion Binary Search Bubble Sort Selection Sort Insertion Sort Merge Sort. Recursion is a technique for defining data structures or algorithms in terms of themselves . A recursive function is a function that "calls itself".

ossie
Télécharger la présentation

Recursion & Sort Algorithms

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. Recursion & Sort Algorithms Recursion Binary Search Bubble Sort Selection Sort Insertion Sort Merge Sort

  2. Recursion is a technique for defining data structures or algorithms in terms of themselves. A recursive function is a function that "calls itself". In function definition, call to same function Recursive functions can be useful in solving problems that can be broken down into smaller or simpler subproblems of the same type. A base case should eventually be reached, at which time the breaking down (recursion) will stop. Has limitations Introduction to Recursion

  3. A recursive function is a function which calls itself somewhere in the function body. Recursive functions are supported in most modern programming languages including C++ The role of recursive functions in programming is to break complex problems down to a solvable problem. The solvable problem is known as the base case. A recursive function is designed to terminate when it reaches its base case. With each recursive call, the parameter controlling the recursion should move closer to the base case Eventually, the parameter reaches the base case and the chain of recursive calls terminates Recursive Functions

  4. Divide and Conquer Basic design technique Break large task into subtasks Subtasks could be smaller versions of the original task! When they are, this is recursion Recursive void Functions

  5. Consider task: Search list for a value Subtask 1: search 1st half of list Subtask 2: search 2nd half of list Subtasks are smaller versions of original task! When this occurs, recursive function canbe used. Usually results in "elegant" solution Recursive void Function Example

  6. Recursive void Functions You might receive a segmentation fault. This happens because each activation record of a function is pushed onto the run-time stack. When you have a function that runs indefinitely, you will run out of space. *This calls for the addition of base case to eliminate infinite recursion. To terminate a recursive function you always need a base case. You may have multiple base cases. Displaying numbers recursively void display(int n) { cout << n << " "; //Call display but with (n-1) display(n-1); return; } Example: display(10); Produces: 10 9 8 7 6 5 4 ... and would never stop. Why? We never told the function when to terminate.

  7. Displaying numbers recursively the function terminate when n is equal (=) to 0 void printnum(int n) { // Base case if ( n == 0 ) return; cout << n << " "; //Recursive call that reduces //input into a smaller case printnum(n-1); return; } Example: printnum(3); Produces: 3 2 1 Example: printnum(-2)? Produces: Infinite recursion! Never reaches the base case, therefore infinite recursion. In order to avoid this behavior, its essential to create robust base cases that allow recursive functions to terminate.

  8. Displaying numbers recursively Improved base case: the function terminate when n is less than or equal (<=) to 0 void display(intn) { //Base case if ( n <= 0 ) return; cout << n << " "; // Recursive call that reduces //input into a smaller case //Will call display but with (n-1) display(n-1); //makes sure to reduce the input //into a smaller case each //call (otherwise, the base case //is not reached). return; } Example: display(10); Produces: 10 9 8 7 6 5 4 3 2 1

  9. How recursion works in C++ Each function creates an activation record on the stack. As a function gets called, it gets added to the top of the stack. Until the function is terminated, is then taken off the stack. A visual example for the display function: The first function called was display(10) which is the activation record seen at the bottom of our stack. The function keeps getting called as long as the base case isn’t met. Up until n = -1, then the activation record of the functions are popped off.

  10. void printrev(int n) { //Terminating condition if ( n <= 0 ) return; //Previous location of our print statement //Calls itself with (n-1) printrev(n-1); //Prints number n cout << n << " "; return; } Example: printrev(3); Produces: 0 1 2 3

  11. Types of Recursion • Direct recursion • a function calls itself • Indirect recursion • function A calls function B, and function B calls function A. Or, • function A calls function B, which calls …, which calls function A

  12. Recursion • A useful strategy to solve recursive functions is to write down the problem on a piece of paper and identify how you are going to break down the problem into a smaller instance and when to terminate (base cases). • A good way to visualize recursion is to draw the activation records by drawing the run time stack of a simple recursive call.

  13. Recursion not always "necessary" Not even allowed in some languages Any task accomplished with recursion canalso be done without it Nonrecursive: called iterative, using loops Recursive: Runs slower, uses more storage Elegant solution; less coding Recursion Versus Iteration

  14. Recursive Binary Search • The binary search algorithm can be expressed as: If array[middle] equals the search value, then the value is found.  Else, if array[middle] is less than the search value, perform a binary search on the upper half of the array.  Else, if array[middle] is greater than the search value, perform a binary search on the lower half of the array. • Recursive function to search array • Determines IF item is in list, and if so: • Where in list it is • Assumes array is sorted • Breaks list in half • Determines if item in 1st or 2nd half • Then searches again just that half • Recursively (of course)! http://linuxconfig.org/example-of-c-binary-search-algorithm

  15. Sorting

  16. Sorting • Sorting is the process of arranging data in a data structure such that it is in increasing or decreasing order of some key in the data. • Simple Algorithms • Bubble Sort • Selection Sort • Insertion Sort • Merge Sort

  17. Compares adjacent array elements Exchanges their values if they are out of order Smaller values bubble up to the top of the array Larger values sink to the bottom Bubble Sort

  18. Sorting an Array of Integers • The picture shows an array of six integers that we want to sort from smallest to largest [0][1] [2] [3] [4] [5]

  19. The Bubble Sort Algorithm • 0 1 2 3 4 • 20 50 60 10 • 40 40 50 50 60 10 60 • 20 40 50 10 60 • 20 40 40 50 10 50 • 20 40 10 5060 Original list After Pass 1 After Pass 2

  20. The Bubble Sort Algorithm 0 1 2 3 4 20 40 10 5060 20 40 10 4020 10 40506010 2010 20405060 After Pass 2 After Pass 3 After Pass 4

  21. Bubble Sort Example

  22. Bubble Sort (cont’d) • To sort an array with k elements, Bubble sort requires k – 1 passes. • Example:

  23. do Initialize exchanges to false for each pair of adjacent array elements if values are out of order Exchange the values Set exchanges to true while exchanges Excellent performance in some cases But very poor performance in others! Works best when array is nearly sorted to begin with Bubble Sort Algorithm http://login2win.blogspot.com/2011/06/bubble-sort-in-c.html

  24. Selection Sorting

  25. A relatively easy to understand algorithm Incrementalalgorithmprinciple Sorts list by Finding smallest (or equivalently largest) element in the list Moving it to the beginning (or end) of the list by swapping it with element in beginning (or end) position Sorts an array in passes Each pass selects the next smallest element At the end of the pass, places it where it belongs Selection Sort

  26. 35 65 30 60 20 scan 0-4, smallest 20 swap 35 and 20 20 65 30 60 35 scan 1-4, smallest 30 swap 65 and 30 20 30 65 60 35 scan 2-4, smallest 35 swap 65 and 35 20 30 35 60 65 scan 3-4, smallest 60 swap 60 and 60 20 30 35 60 65 done Selection Sort Example

  27. Selection Sort (cont’d) • To sort an array with k elements, Selection sort requires k – 1 passes. • Example:

  28. The Selectionsort Algorithm • Start by finding the smallest entry. [0][1] [2] [3] [4] [5]

  29. The Selectionsort Algorithm • Start by finding the smallest entry. • Swap the smallest entry with the first entry. [0][1] [2] [3] [4] [5]

  30. The Selection sort Algorithm • Start by finding the smallest entry. • Swap the smallest entry with the first entry. [0][1] [2] [3] [4] [5]

  31. The Selection sort Algorithm • Part of the array is now sorted. Sorted side Unsorted side [0][1] [2] [3] [4] [5]

  32. The Selection sort Algorithm • Find the smallest element in the unsorted side. Sorted side Unsorted side [0][1] [2] [3] [4] [5]

  33. The Selection sort Algorithm • Find the smallest element in the unsorted side. • Swap with the front of the unsorted side. Sorted side Unsorted side [0][1] [2] [3] [4] [5]

  34. The Selection sort Algorithm • We have increased the size of the sorted side by one element. Sorted side Unsorted side [0][1] [2] [3] [4] [5]

  35. The Selection sort Algorithm • The process continues... Sorted side Unsorted side Smallest from unsorted [0][1] [2] [3] [4] [5]

  36. The Selection sort Algorithm • The process continues... Sorted side Unsorted side Swap with front [0][1] [2] [3] [4] [5]

  37. The Selection sort Algorithm Sorted side is bigger • The process continues... Sorted side Unsorted side [0][1] [2] [3] [4] [5]

  38. The Selection sort Algorithm • The process keeps adding one more number to the sorted side. • The sorted side has the smallest numbers, arranged from small to large. Sorted side Unsorted side [0][1] [2] [3] [4] [5]

  39. Sorted side Unsorted side The Selection sort Algorithm • We can stop when the unsorted side has just one number, since that number must be the largest number. [0][1] [2] [3] [4] [5]

  40. The Selection sort Algorithm • The array is now sorted. • We repeatedly selected the smallest element, and moved this element to the front of the unsorted side. [0][1] [2] [3] [4] [5]

  41. insertion Sorting

  42. Reduces number of key comparisons made in selection sort Sorts list by Finding first unsorted element in list Moving it to its proper position Insertion Sort

  43. Based on technique of card players to arrange a hand Player keeps cards picked up so far in sorted order When the player picks up a new card Makes room for the new card Then inserts it in its proper place Insertion Sort

  44. For each element from 2nd (nextPos = 1) to last: Insert element at nextPos where it belongs Increases sorted subarray size by 1 To make room: Hold nextPos value in a variable Shuffle elements to the right until gap at right place Insertion Sort Algorithm

  45. Insertion Sort Example

  46. Insertion Sort (cont’d) • To sort an array with k elements, Insertion sort requires k – 1 passes. • Example:

  47. The Insertion sort Algorithm • The Insertionsort algorithm also views the array as having a sorted side and an unsorted side. [0][1] [2] [3] [4] [5]

  48. Sorted side Unsorted side The Insertion sort Algorithm • The sorted side starts with just the first element, which is not necessarily the smallest element. [0][1] [2] [3] [4] [5]

  49. Sorted side Unsorted side The Insertion sort Algorithm • The sorted side grows by taking the front element from the unsorted side... [0][1] [2] [3] [4] [5]

  50. Sorted side Unsorted side The Insertion sort Algorithm • ...and inserting it in the place that keeps the sorted side arranged from small to large. [0][1] [2] [3] [4] [5]

More Related