Consider the procedure implementing quicksort:
Quicksort(A,p,r){
if (p < r):
q = Partition(A,p,r)
Quicksort(A,p,q-1)
Quicksort(A,q+1,r)
}
If instead of calling Quicksort(A,p,q-1) , the call is Quicksort(A,p,q), What would be the consequence on correctness or running time when:
a) The array is already sorted
b) The array is filled with n copies of the same value
******************************************************************************************
Please Upvote the answer as it matters to me a lot :)
*****************************************************************************************
As per HomeworkLib expert answering guidelines,Experts are supposed to
answer only certain number of questions/sub-parts in a post.Please
raise the remaining as a new question as per HomeworkLib
guidelines.
******************************************************************************************
the correctness will be the same, it sorts the array but might fall in an infinite loop
an example: 1,2,3,4
the pivot is 4 and instead of the call p=0 r=3
we will q=3 so while the call Quicksort(A,p,q) the same array is called again
and again until it continues to be infinite
Consider the procedure implementing quicksort: Quicksort(A,p,r){ if (p < r): q = Partition(A,p,r) Quicksort(A,p,q-1) Quicksort(A,q+1,r) }...
Implement quicksort and bucket sort. Use the code in your book to help; the partition in quicksort is tricky. Make sure your implementations are correct — it is easy to gain some confidence in the correctness of your code by writing a program which creates arrays filled with random numbers, sorts them, and then checks that they are sorted. Then time your code (using clock() or similar methods) on both methods for arrays filled with random integers of the following...
And the related
algorithms:
(20 points) Consider the following strategy for choosing a pivot element for the Partition subroutine of QuickSort, applied to an array A. .Let n be the number of elements of the array A. If n 24, perform an Insertion Sort of A and return. Otherwise: Choose 2n/2)| elements at random from n; let S be the new list with the chosen elements. Sort the list S using Insertion Sort and use the median m of S...
Suppose the quicksort program shown uses a partition function that always picks a[m] as the 5B. 2M separator v. When the array a[m],....a[n] is reordered, assume that the order is preserved as much as possible. That is, first come all the elements less than v, in their original order, then all elements equal to v, and finally all elements greater than v, in their original order. int a[11]; void readArray0 {/* Reads 9 integers into int I; void quicksort( int...
C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble with is in bold. -------------------------------------------------------------------------------------------------driverProgram.cpp #include #include #include #include #include "quickSort.cpp" using namespace std; int main() { const int MIN_SIZE = 4; //Array size const int SIZE = 25; int theArray[SIZE] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 20, 21, 22, 23, 24, 25}; cout << "List of 25 items: ";...
3 Quicksort 10 points (5 points each) 1. Suppose that you are given an array A[1..n] and that you want to sort it using quicksort. Further suppose that your algorithm could consult an oracle to predict what element to use as the pivot. Which element would it pick so that your algorithm would run as fast as possible? What is the running time given your pivot? 2. Run the partition algorithm to partition the array A (6,7,2,4, 10,8, 1,9)
Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array of integers q ≤ r and q,r ∈ N. Postcondition: Returns the smallest element of A[q, ... , r]. 1: function Smallest (A , q , r) 2: if q = r then 3: return A[q] 4: else 5: mid <--- [q+r/2] 6: return min (Smallest(A, q, mid), Smallest (A, mid + 1, r)) 7: end if 8: end function (a) Write a recurrence...
Sorting algorithm: quick sort
Exercise One (20 marks) Given the following program body for implementing Quick sort, complete the program by writing code where required import java.util.Random; public class QuickSort public void quickSectlinti] A) QuickSort/A, O, A.length-1); private void guickSortlin Aiat low.int high) //Complete the code for the quicksort method (5 marks] private void swaplint[] a, int indexl, int index2) //Complete the code for the swap method [3 marks] private int setPivotlint low, int high) Random rand = new Random();...
1. When implementing a stack on an array, where is a new item added? Select one: a. At index 0 and the rest of the stack is moved up. b. The next available slot (index n when there are already n items). c. The next available slot (index n+1 when there are already n items). d. The end of the array. 2. When implementing a queue on an array, consider one item already in in the queue. What happens to...
Question 5 (10 points) Below is the PARTITION(A, p. r) pseudocode where pivot is always selected as the last element and array A=(-23,7,-14,1,5,1). a. Illustrate the PARTITIONCA, P.7) module operation like below when call it with the above array A. Show clearly the position of i and ; in each picture. Pj PARTITIONCA, p,r) 1. x=A[r] 2. i=p - 1 3. for j =p tor-1 4. if Allsx 5. i=i+1 exchange A[i] with AD 7. exchange A[i+1] with 4[r] 8....
1. (10 pts total) For parts (1a) and (1b), justify your answers in terms of deterministic QuickSort, and for part (1c), refer to Randomized QuickSort. In both cases, refer to the versions of the algorithms given in the lecture notes for Week 3. (a) (3 points) What is the asymptotic running time of QuickSort when every element of the input A is identical, i.e., for 1 ≤ i,j ≤ n, A[i] = A[j]? Prove your answer is correct. (b) (3...