Answered step by step
Verified Expert Solution
Question
1 Approved Answer
2. (4pts) Stack depth for QUICKSORT - The QUICKSORT algorithm of Section 7.1 contains two recursive calls to itself. After the call to PARTITION, the
2. (4pts) Stack depth for QUICKSORT - The QUICKSORT algorithm of Section 7.1 contains two recursive calls to itself. After the call to PARTITION, the left sub array is recursively sorted and then the right subarray is recursively sorted. The second recursive call in QUICKSORT is not really necessary; it can be avoided by using an iterative control structure. Good compilers provide this technique, called tail recursion.. Consider the following version of quick sort, which simulates tail recursion. 2a) Argue that QUICKSORT ' (A, 1, length [A]) correctly sorts the array A.(1pt) 2b) Compilers usually execute recursive procedures by using a stack that contains pertinent information, including the parameter values, for each recursive call. The information for the most recent call is at the top of the stack, and the information for the initial call is at the bottom. When a procedure is invoked, its information is pushed onto the stack; when it terminates, its information is popped. Since we assume that array parameters are represented by pointers, the information for each procedure call on the stack requires O(1) stack space. The stack depth is the maximum amount of stack space used at any time during a computation. Describe a scenario in which the stack depth of QUICKSORT' is (n) on an n-element input array.(1pt) 2c) Modify the code for QUICKSORT' so that the worst-case stack depth is (lgn). Maintain the O(nlgn) expected running time of the algorithm.(2pts)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started