CMPT 102 Introduction to Scientific Computer Programming Recursion

![Introduction to Recursion ] A function that "calls itself" S Said to be recursive Introduction to Recursion ] A function that "calls itself" S Said to be recursive](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-2.jpg)
![Functions and Recursion ] Divide and Conquer S Well known design method S Task Functions and Recursion ] Divide and Conquer S Well known design method S Task](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-3.jpg)
![Recursive void Function ] As and example consider the following task: ] Print a Recursive void Function ] As and example consider the following task: ] Print a](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-4.jpg)
![Recursive problem ] Consider task: ] Print a triangle built of N rows of Recursive problem ] Consider task: ] Print a triangle built of N rows of](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-5.jpg)
![Print triangle: Recursive Design ] Break problem into two cases ] Simple/base case: if Print triangle: Recursive Design ] Break problem into two cases ] Simple/base case: if](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-6.jpg)

![print. Triangle: execution ] Example calling program calls print. Triangle(3); (which calls) print. Triangle(2); print. Triangle: execution ] Example calling program calls print. Triangle(3); (which calls) print. Triangle(2);](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-8.jpg)
![Recursion—Details (1) ] Each time a function is called 1. The execution of the Recursion—Details (1) ] Each time a function is called 1. The execution of the](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-9.jpg)


![Recursion Big Picture ] Outline of successful recursive function: S One or more cases Recursion Big Picture ] Outline of successful recursive function: S One or more cases](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-12.jpg)
![Infinite Recursion ] Base case MUST eventually be entered S If base case is Infinite Recursion ] Base case MUST eventually be entered S If base case is](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-13.jpg)
![Infinite Recursion Example ] Consider alternate function definition: void print. Triangle. OOPS(int n) { Infinite Recursion Example ] Consider alternate function definition: void print. Triangle. OOPS(int n) {](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-14.jpg)
![What is a Stack? ] A stack is a specialized memory structure S Think What is a Stack? ] A stack is a specialized memory structure S Think](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-15.jpg)
![Recursion and Stacks (1) ] Recursion uses stacks. Consider a recursive function (example: print. Recursion and Stacks (1) ] Recursion uses stacks. Consider a recursive function (example: print.](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-16.jpg)




![Stack Overflow ] Size of stack limited S Memory is finite, too many frames Stack Overflow ] Size of stack limited S Memory is finite, too many frames](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-21.jpg)
![Recursion Vs Iteration ] Recursion not always an optimal solution ] Not all languages Recursion Vs Iteration ] Recursion not always an optimal solution ] Not all languages](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-22.jpg)
![Recursive Functions with Return Values ] Recursion not limited to void functions as in Recursive Functions with Return Values ] Recursion not limited to void functions as in](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-23.jpg)
![Powers ] For example, consider C library function pow(): ] two. Cubed = pow(2. Powers ] For example, consider C library function pow(): ] two. Cubed = pow(2.](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-24.jpg)

![Calling Function power() ] Example calls: ] power(2, 0); executes stopping case: return 1; Calling Function power() ] Example calls: ] power(2, 0); executes stopping case: return 1;](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-26.jpg)
![Calling Function power() ] Another example: power(2, 3); iterative case: power(2, 2)*2 iterative case: Calling Function power() ] Another example: power(2, 3); iterative case: power(2, 2)*2 iterative case:](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-27.jpg)
![Thinking Recursively: factorial ] Consider n! = 1 * 2 * … * n Thinking Recursively: factorial ] Consider n! = 1 * 2 * … * n](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-28.jpg)
![Recursive Design Process ] Don’t trace entire recursive sequence! ] Just check 3 properties: Recursive Design Process ] Don’t trace entire recursive sequence! ] Just check 3 properties:](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-29.jpg)
![Recursive Design Check: fact() ] Check fact() against 3 properties: 1. No infinite recursion: Recursive Design Check: fact() ] Check fact() against 3 properties: 1. No infinite recursion:](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-30.jpg)

![Recursion: Quick sort ] Recall our discussion of sorting, (in the unit on arrays) Recursion: Quick sort ] Recall our discussion of sorting, (in the unit on arrays)](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-32.jpg)
![Quicksort function: void Quicksort( int In. Array[], int lo. Bound, int hi. Bound ) Quicksort function: void Quicksort( int In. Array[], int lo. Bound, int hi. Bound )](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-33.jpg)
![Sample Quick Sort: 1 ] Unsorted Array passed into first call to Quicksort (call Sample Quick Sort: 1 ] Unsorted Array passed into first call to Quicksort (call](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-34.jpg)
![Sample Quick Sort: 2 ] ] When Quicksort is called for the left part, Sample Quick Sort: 2 ] ] When Quicksort is called for the left part,](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-35.jpg)
![Sample Quick Sort: 3 ] ] When Quicksort is called for the left part Sample Quick Sort: 3 ] ] When Quicksort is called for the left part](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-36.jpg)
![Sample Quick Sort: 4 ] ] When Quicksort returns from sorting left part 2, Sample Quick Sort: 4 ] ] When Quicksort returns from sorting left part 2,](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-37.jpg)
![Sample Quick Sort: 5 ] ] ] When Quicksort is called for the left Sample Quick Sort: 5 ] ] ] When Quicksort is called for the left](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-38.jpg)
![Sample Quick Sort: 6 ] ] When Quicksort returns from sorting left part 3, Sample Quick Sort: 6 ] ] When Quicksort returns from sorting left part 3,](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-39.jpg)
![Sample Quick Sort: 7 ] ] When Quicksort is called for the left part Sample Quick Sort: 7 ] ] When Quicksort is called for the left part](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-40.jpg)
![Sample Quick Sort: 8 ] ] When Quicksort returns from sorting left part 4, Sample Quick Sort: 8 ] ] When Quicksort returns from sorting left part 4,](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-41.jpg)
![Sample Quick Sort: 9 ] ] ] When Quicksort returns from sorting right part Sample Quick Sort: 9 ] ] ] When Quicksort returns from sorting right part](https://slidetodoc.com/presentation_image_h2/865f7c2edb0c02f4d5855a759b1da818/image-42.jpg)
- Slides: 42
CMPT 102 Introduction to Scientific Computer Programming Recursion 1/21/2022 © Janice Regan, CMPT 102, Sept. 2006 0
Introduction to Recursion ] A function that "calls itself" S Said to be recursive S In function definition, call to same function ] C allows recursion S As do most high-level languages S Can be useful programming technique S Has limitations © Janice Regan, CMPT 102, Sept. 2006 1
Functions and Recursion ] Divide and Conquer S Well known design method S Task is broken into smaller subtasks ] Subtasks can be independent parts of the larger task ] Subtasks can be copies of the same task S Use recursion when the subtasks are smaller versions of the same task! © Janice Regan, CMPT 102, Sept. 2006 2
Recursive void Function ] As and example consider the following task: ] Print a triangle built of N rows of *s, Row zero will hold 1 *, Row N will have 2 N+1 *s * ] N=5 ********* © Janice Regan, CMPT 102, Sept. 2006 * N=3 ***** 3
Recursive problem ] Consider task: ] Print a triangle built of N rows of *s S Subtask 1: print first (N-1) rows of *s S Subtask 2: print Nth row of *s (2 N+1 *s) ] Subtasks are smaller versions of original task! S Each task involves printing a row (or rows) or *s ] Therefore, the problem is of a type that can be usefully solved using a recursive method S Can use this observation to build an ‘elegant’ solution © Janice Regan, CMPT 102, Sept. 2006 4
Print triangle: Recursive Design ] Break problem into two cases ] Simple/base case: if n=0 S print one * to the screen ] Recursive case: if n>0, two subtasks: 1 - print the first n-1 rows of the triangle 2 - print the nth row of the triangle (2 N+1 *s) ] Example: argument 4: S 1 st subtask prints 3 rows of *s (as show on slide 3) S 2 nd subtask displays 4 © Janice Regan, CMPT 102, Sept. 2006 5
print. Triangle function void print. Triangle(int N) { if( N <= 0) { return } if (N == 1) { printf(“*n”); } else { print. Triangle(N-1); for(i=0; i<N; i++) { printf(“**”); } printf(“*n”); } } © Janice Regan, CMPT 102, Sept. 2006 //Base case //Recursive step 6
print. Triangle: execution ] Example calling program calls print. Triangle(3); (which calls) print. Triangle(2); (which calls) print. Triangle(1); Which prints a row with 1 * and a newline then returns Which prints a row with 3 *s and a newline then returns Which prints a row with 5 *s and a newline then returns ] Notice 1 st two calls call print. Triangle recursively © Janice Regan, CMPT 102, Sept. 2006 7
Recursion—Details (1) ] Each time a function is called 1. The execution of the calling function stops to wait for the results of the called function to be returned 2. A new frame (area in memory) is assigned for the called function, arguments are copied in, and the called function begins executing. 3. At some point (points) in the execution of the called function, the called function will call itself 4. The execution of the called function stops to wait for the results of the recursive call to itself. © Janice Regan, CMPT 102, Sept. 2006 8
Recursion—Details (2) 5. A new frame is assigned for the recursive call, arguments are copied, and the recursive call to the function begins executing. 6. The process continues with successive recursive calls (repetition of steps 3 -5), until one of the recursive calls executes the stopping case inside the recursive function. The stopping case does not call the recursive function again but instead returns the answer for the stopping case to its calling function © Janice Regan, CMPT 102, Sept. 2006 9
Recursion—Details (3) 5. The calling function takes the returned value and completes its own execution returning a value to its own calling function 6. Step 7 is repeated until the final answer is returned to the original calling function © Janice Regan, CMPT 102, Sept. 2006 10
Recursion Big Picture ] Outline of successful recursive function: S One or more cases where function accomplishes it’s task by making one or more recursive calls to solve smaller versions of the original task l Called recursive cases S One or more cases where function accomplishes it’s task without recursive calls l Called base cases or stopping cases © Janice Regan, CMPT 102, Sept. 2006 11
Infinite Recursion ] Base case MUST eventually be entered S If base case is not reached (an error in design or implementation has occurred) then the recursion will consider forever S Recursive calls will never end! ] Remember the print. Triangle example: S Base case: print a single * and return S Recursive calling stopped when base case executed © Janice Regan, CMPT 102, Sept. 2006 12
Infinite Recursion Example ] Consider alternate function definition: void print. Triangle. OOPS(int n) { print. Triangle. OOPS(N-1); for(i=0; i<N; i++) { printf(“**”); } printf(“*n”); } } ] Seems reasonable at first glance: BUT S S S There is not base case Recursion never stops: print. Triangle keeps calling itself for ever The triangle is never printed (never get to the for loop) © Janice Regan, CMPT 102, Sept. 2006 13
What is a Stack? ] A stack is a specialized memory structure S Think of a stack as like a stack of sheets of paper. l You always place a new sheet of paper on top of the stack, l when you need a piece of paper from the stack, you take it from the top of the stack. S Called "last-in/first-out" memory structure © Janice Regan, CMPT 102, Sept. 2006 14
Recursion and Stacks (1) ] Recursion uses stacks. Consider a recursive function (example: print. Triangle(M) ). Consider The first call to print. Triangle (made from the calling program) will be called print. Triangle 0 and has argument M S When this call to print. Triangle (print. Triangle 0) is executed a frame is created in which print. Triangle 0 executes. S print. Triangle 0 executes in this frame until it calls print. Triangle, then it stops and waits for the value to be returned from the recursive call to print. Triangle (print. Triangle 1) S The frame for print. Triangle 0 is placed on the stack of frames S © Janice Regan, CMPT 102, Sept. 2006 15
Recursion and Stacks (2) S The second call to print. Triangle, the first recursive call, (made in print. Triangle 0) will be print. Triangle 1 and has argument M-1 S When this call to print. Triangle (print. Triangle 1) is executed a frame is created in which print. Triangle 1 executes. S print. Triangle 1 executes in this frame until it calls itself, then it stops and waits for the value to be returned from the recursive call to print. Triangle (print. Triangle 2) S The frame for print. Triangle 1 is placed on the stack of frames © Janice Regan, CMPT 102, Sept. 2006 16
Recursion and Stacks (3) S The third call to print. Triangle (made from print. Triangle 1) will be print. Triangle 2 and has argument M-2 S When this call to print. Triangle (print. Triangle 2) is executed a frame is created in which print. Triangle 2 executes. S print. Triangle 2 executes in this frame until it calls itself, then it stops and waits for the value to be returned from the recursive call to print. Triangle (print. Triangle 3) S The frame for print. Triangle 2 is placed on the stack of frames © Janice Regan, CMPT 102, Sept. 2006 17
Recursion and Stacks (4) S This pattern continues until the M-1 st call to print. Triangle (made from print. Triangle(M-2)) called print. Triangle(M-1) and has argument M -M+1=1 S When this call to print. Triangle (print. Triangle. M -1) is executed a frame is created in whiich print. Triangle. M-1 executes S print. Triangle. M-1 executes in this frame, since this is the stopping case for print. Triangle, a single * is printed and the function terminates returning control to the function which called it. © Janice Regan, CMPT 102, Sept. 2006 18
Recursion and Stacks (5) S The previous frame is taken off the top of the stack. The loop following the recursive call is executed and prints 3 *s. The frame is then destoyed returning control to the function which called it S The previous frame is taken off the top of the stack. The loop following the recursive call is executed and prints 5 *s. The frame is then destoyed returning control to the function which called it S This pattern continues until all frames in the stack have been executed and the triangle has been completely printed © Janice Regan, CMPT 102, Sept. 2006 19
Stack Overflow ] Size of stack limited S Memory is finite, too many frames in the stack will cause memory to be exhausted S Each recursive call adds a frame to the stack so a limited number of recursive calls can be supported by a stack of a particular size. ] Infinite recursion always causes the stack to overflow (memory to be exhausted) ] Too many levels of recursion (not infinite) can also cause the stack to overflow. © Janice Regan, CMPT 102, Sept. 2006 20
Recursion Vs Iteration ] Recursion not always an optimal solution ] Not all languages permit recursion ] Any algorithm implemented using recursion can also be implemented iteratively, using loops ] Recursive implementations : S Runs slower, uses more storage S Elegant solution; less coding, less complications, faster and more straightforward to implement © Janice Regan, CMPT 102, Sept. 2006 21
Recursive Functions with Return Values ] Recursion not limited to void functions as in our example of print. Triangle ] Can have a recursive function with a return value of any type ] Use same technique as for void functions 1. One or more cases iterative cases l Should be smaller cases of the entire problem 2. One or more base cases © Janice Regan, CMPT 102, Sept. 2006 22
Powers ] For example, consider C library function pow(): ] two. Cubed = pow(2. 0, 3. 0); S Returns 2 raised to power 3 (8. 0) S Takes two double arguments S Returns double value ] Let’s write our own power() function S Our power() function will be implemented as a recursive function int power(int a, int b); © Janice Regan, CMPT 102, Sept. 2006 23
Definition for power() int power(int x, int n) { } if (n<DBL_EPSILON) { printf( "Illegal argumentn“); exit(1); } if (n>DBL_EPSILON) { return (power(x, n-1)*x); } else { return (1); } © Janice Regan, CMPT 102, Sept. 2006 24
Calling Function power() ] Example calls: ] power(2, 0); executes stopping case: return 1; ] power(2, 1); executes iterative case: return (power(2, 0) * 2); this requires evaluating power(2, 0) evaluates stopping case returns 1; Now complete evaluation of iterative case return 1*2 © Janice Regan, CMPT 102, Sept. 2006 25
Calling Function power() ] Another example: power(2, 3); iterative case: power(2, 2)*2 iterative case: power(2, 1)*2 iterative case: power(2, 0)*2 base case: 1 return 1*2 return 2*2 return 4*2 ] Recursion stops at base case ] Values "returned back" up stack © Janice Regan, CMPT 102, Sept. 2006 26
Thinking Recursively: factorial ] Consider n! = 1 * 2 * … * n ] Recursive definition of factorial: int fact(int n) ] Define recursive case S Given the value of (n-1)!, what is the value of n! n! = n* (n-1)! ] Define stopping case (or cases) S 0! =1 S 1! =1 S Factorial of a negative number is not defined so 0 is the smallest number that needs to be considered © Janice Regan, CMPT 102, Sept. 2006 27
Recursive Design Process ] Don’t trace entire recursive sequence! ] Just check 3 properties: 1. No infinite recursion 2. Stopping cases return correct values 3. Recursive cases return correct values © Janice Regan, CMPT 102, Sept. 2006 28
Recursive Design Check: fact() ] Check fact() against 3 properties: 1. No infinite recursion: l argument decreases by 1 each call l Eventually must get to base case of 1 2. Stopping case returns correct value: l fact(0) = fact(1) = 1 is base case l Returns 1, which is correct for 0! or 1! 3. Recursive calls correct: l For n>1, fact(n) returns fact(n-1)*n l Plug in sample value to verify © Janice Regan, CMPT 102, Sept. 2006 29
fact() int fact( ) { if (n < 0 ) { return -999; } /* indicates incorrect input */ if (n == 0 || n == 1) { return 1; /* stopping case */ } else { return ( fact(n-1) * n ); /* iterative case */ } } © Janice Regan, CMPT 102, Sept. 2006 30
Recursion: Quick sort ] Recall our discussion of sorting, (in the unit on arrays) ] Quicksort is a recursive algorithm ] Each time the Quicksort function is called S the array to be sorted is divided into two parts, the left part for which all values are less than the pivot value l the right part for which all values are larger than the pivot value S the Quicksort function is then recursively called l First to sort the right part of the array l Again to sort the left part of the array l © Janice Regan, CMPT 102, Sept. 2006 31
Quicksort function: void Quicksort( int In. Array[], int lo. Bound, int hi. Bound ) { /* block of code to break array into two parts */ /* the first part of the array hold all values < pivot */ /* the second part holds all values > pivot */ Quicksort(In. Array, lo. Bound, hi. Swap-1); /* left part */ Quicksort(In. Array, hi. Swap+1, hi. Bound); /* right part */ } © Janice Regan, CMPT 102, Sept. 2006 32
Sample Quick Sort: 1 ] Unsorted Array passed into first call to Quicksort (call 0) A[0] A[1] A{2] A[3] A[4] 23 3 12 19 7 A[5} A[6] A[7] A[8] 75 48 95 37 Pivot, middle element A[9] A[10] A[11] A[12] 86 5 16 32 ] The array is partitioned into left part (all values < pivot) and right part (all values > pivot) 5 3 12 19 7 32 Left part 23 16 37 48 86 95 75 Right part ] Within call 0 Quicksort is called for the left part then for the right part © Janice Regan, CMPT 102, Sept. 2006 33
Sample Quick Sort: 2 ] ] When Quicksort is called for the left part, the execution of the present function (call 0 ) stops, the call 0 function frame is placed on the stack, (call 0 sorts A[0]-A[12] ) A new function frame to execute Quicksort on the left part of the array is created. Execution of call 1 begins (call 1 sorts left part, A[0]A[8]). Call 1 partitions Left part into two sections, left part 2 (all values < pivot of left part) and right part 2 (all values > pivot of left part) A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] 5 3 7 19 37 16 32 23 12 Left part 2 Right part 2 A[9] A[10] A[11] A[12] 48 86 95 75 Right part ] Within call 1 Quicksort is called for the left part 2 then for the right part 2 © Janice Regan, CMPT 102, Sept. 2006 34
Sample Quick Sort: 3 ] ] When Quicksort is called for the left part 2, the execution of the present function (call 1 ) stops, the call 1 function frame is placed on the stack. (call 1 sorts A[0]-A[8] ) A new function frame to execute Quicksort on the left part 2 of the array is created. Execution of call 2 begins (call 2 sorts left part 2, A[0]-A[1]). Since there are only two elements in left part 2 this is a stopping case. The elements are put in order and Quicksort returns. A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] 3 5 7 19 37 16 32 23 12 Right part 2 ] A[9] A[10] A[11] A[12] 48 86 95 75 Right part The frame created for call 2 is destroyed and the last frame added to the stack is reactivated (call 1) © Janice Regan, CMPT 102, Sept. 2006 35
Sample Quick Sort: 4 ] ] When Quicksort returns from sorting left part 2, the execution of call 1 is reinitiated. The call 1 function frame is retrieved from the stack (call 1 sorts A[0]-A[8] ). Then call 1 calls Quicksort for right part 2, the execution of call 1 stops, the call 1 function frame is placed back on the stack A new function frame to execute Quicksort on the right part 2 of the array is created. Execution of call 3 begins (call 3 sorts right part 2, A[3]-A[8]). Call 3 partitions right part 2 into two sections, left part 3 (all values < pivot of right part 2) and right part 3 (all values > pivot of right part 2) A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] 3 5 7 12 16 19 32 23 37 Left part 3 ] Right part 3 A[9] A[10] A[11] A[12] 48 86 95 75 Right part Within call 3 Quicksort is called for the left part 3 then for the right part 3 © Janice Regan, CMPT 102, Sept. 2006 36
Sample Quick Sort: 5 ] ] ] When Quicksort is called for the left part 3, the execution of the present function (call 3 ) stops, the call 3 function frame is placed on the stack. (call 3 sorts A[3]-A[8] ) A new function frame to execute Quicksort on the left part 3 of the array is created. Execution of call 4 begins (call 4 sorts left part 3, A[3]-A[3]). Since there is only one element in left part 3 this is a stopping case and Quicksort returns. A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] 3 5 7 12 16 19 32 23 37 Right part 3 A[9] A[10] A[11] A[12] 48 86 95 75 Right part The frame created for call 4 is destroyed and the last frame added to the stack is reactivated (call 3) © Janice Regan, CMPT 102, Sept. 2006 37
Sample Quick Sort: 6 ] ] When Quicksort returns from sorting left part 3, the execution of call 3 is reinitiated. The call 3 function frame is retrieved fro the stack (call 3 sorts A[3]-A[8] ). Then call 3 calls Quicksort for right part 3, the execution of call 3 stops, the call 3 function frame is placed back on the stack A new function frame to execute Quicksort on the right part 3 of the array is created. Execution of call 5 begins (call 5 sorts right part 3, A[5]-A[8]). Call 5 partitions right part 3 into two sections, left part 4 (all values < pivot of right part 2) and right part 4 (all values > pivot of right part 2) A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] 3 5 7 12 16 23 19 32 37 Left part 4 ] A[9] A[10] A[11] A[12] 48 Right part 4 86 95 75 Right part Within call 5 Quicksort is called for the left part 4 then for the right part 4 © Janice Regan, CMPT 102, Sept. 2006 38
Sample Quick Sort: 7 ] ] When Quicksort is called for the left part 4, the execution of the present function (call 5 ) stops, the call 5 function frame is placed on the stack. (call 5 sorts A[5]-A[8] ) A new function frame to execute Quicksort on the left part 4 of the array is created. Execution of call 6 begins (call 6 sorts left part 4, A[5]-A[6]). Since there are only two elements in left part 3 this is a stopping case and Quicksort returns. A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] 3 5 7 12 16 19 23 32 37 A[9] A[10] A[11] A[12] 48 Right part 4 ] 86 95 75 Right part The frame created for call 6 is destroyed and the last frame added to the stack is reactivated (call 5) © Janice Regan, CMPT 102, Sept. 2006 39
Sample Quick Sort: 8 ] ] When Quicksort returns from sorting left part 4, the execution of call 5 is reinitiated. The call 5 function frame is retrieved from the stack (call 5 sorts A[5]-A[8] ). Then call 5 calls Quicksort for right part 4, the execution of call 5 stops, the call 5 function frame is placed back on the stack A new function frame to execute Quicksort on the right part 4 of the array is created. Execution of call 7 begins (call 7 sorts right part 4, A[8]-A[8]). Since there is only one element in right part 4 this is a stopping case and Quicksort returns. A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] 3 5 7 12 16 19 23 32 37 A[9] A[10] A[11] A[12] 48 86 95 75 Right part ] The frame created for call 7 is destroyed and the last frame added to the stack is reactivated (call 5) © Janice Regan, CMPT 102, Sept. 2006 40
Sample Quick Sort: 9 ] ] ] When Quicksort returns from sorting right part 4, the execution of call 5 is reinitiated. The call 5 function frame is retrieved from the stack (call 5 sorts A[5]-A[8] ). Call 5 has now completed both its recursive calls. The frame created for call 5 is destroyed and the last frame added to the stack is reactivated (call 3 sorts A[3]-A[8]) Call 3 has now completed both its recursive calls The frame created for call 3 is destroyed and the last frame added to the stack is reactivated (call 1 sorts A[0]-A[8] ). Then call 1 calls Quicksort for right part, the execution of call 1 stops, the call 1 function frame is placed back on the stack A new function frame to execute Quicksort on the right part of the array is created. Execution of call 8 begins (call 8 sorts right part, A[10]-A[12]). Call 8 partitions right part into two sections, left part 5(all values < pivot of right part 2, no entries) and right part 5 (all values > pivot of right part) Array is now in order © Janice Regan, CMPT 102, Sept. 2006 41