Question

In: Computer Science

(do not need to code) Consider QuickSort on the array A[1:n] andassume that the pivot...

Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to split the array A[lo:hi] into two portions such that all elements in the left portion A[lo:m] are =< x and all elements in the right portion A[m:hi] are >=x) is the first element of the array to be split (i. e., A[lo]).

Construct an infinite sequence of numbers for n and construct an assignment of the numbers 1...n to the n array elements that causes QuickSort, with the stated choice of pivot, to

(a).execute optimally (that is A[lo:m] and A[m:hi] are always of equal size)

(b).execute in the slowest possible way.

Solutions

Expert Solution

Algorithm:-

Step 1. Choosing the Pivot Element

->>>pivot element can determine the complexity of the algorithm i.e. whether it will be n*logn or quadratic time:

(i). we choose the low element as pivot.
This can harm us badly as the pivot might end up to be the smallest or the largest element, thus leaving one of the partitions empty.

(ii). We should choose the Median of the first, last and middle elements.
->If there are N elements, then the ceiling of N/2 is taken as the pivot element.

Example:

8, 3, 25, 6, 10, 17, 1, 2, 18, 5

First element: 8
Middle element: 10
Last element: 5

Therefore the median on [8,10,5] is 8.

Step 2. Partitioning

a. First thing is to get the pivot out of the way and swapping it with the last number.

Example: (shown using the above array elements)

5, 3, 25, 6, 10, 17, 1, 2, 18, 8

b. Now we want the elements greater than pivot to be on the right side of it and similarly the elements less than pivot to be on the left side of it.

For this we define 2 pointers, namely index1 and index2.
->>>index1 being at the first index and index2 being at the last index of the array.

   * While index1 is less than index2 we keep in incrementing index1 until we find an element    greater than pivot.
   * Similarly, while index2 is greater then index1 keep decrementing index2 until we find an element less than pivot.
   * After both index1 and index2 stop we swap the elements at the indexes of index1 and index2 respectively.


c. Restoring the pivot

When the above steps are done correctly we will get this as our output:

[5, 3, 2, 6, 1] [8] [10, 25, 18, 17]

Step 3. Recursively Sort the left and right part of the pivot.


Related Solutions

. Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to...
. Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to split the array A[lo:hi] into two portions such that all elements in the left portion A[lo:m] are ≤x and all elements in the right portion A[m:hi] are ≥x) is the second element of the array to be split (i. e., A[lo+1]). For a specific value of n at least 13 (your choice), construct an assignment of the numbers 1...n to the n array elements...
Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to split...
Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to split the array A[lo:hi] into two portions such that all elements in the left portion A[lo:m] are ≤x and all elements in the right portion A[m:hi] are ≥x) is the second element of the array to be split (i. e., A[lo+1]). For a specific value of n at least 13 (your choice), construct an assignment of the numbers 1...n to the n array elements that...
What is the running time of QUICKSORT on an array A of length n containing all...
What is the running time of QUICKSORT on an array A of length n containing all the same values? What is the running time if A contains distinct elements sorted in decreasing order? Explain your answers in detail, referring to the source code for PARTITION and QUICKSORT.
Write an implementation of quicksort where the pivot is selected randomly.
Write an implementation of quicksort where the pivot is selected randomly.
in C++, Modify the quicksort algorithm such that it uses the last item as the pivot...
in C++, Modify the quicksort algorithm such that it uses the last item as the pivot instead of the 1st. Also, sort in descending order, instead of ascending order. NOTE: Do not move the last element into the first element of the array. You must treat the algorithm as if the pivot is actually sitting in the last location of the array. After it has been sorted in descending order, go through all the items in the array and make...
c++ I need a code that will fill an array size of 1000, an array of...
c++ I need a code that will fill an array size of 1000, an array of size 2000, and an array size of 10000, with random int values. Basically like this: array1[1000] = filled all with random numbers array2[2000] = filled all with random numbers array3[10000] = filled all with random numbers C++ no need for print
P1 Consider an array of length n that contains unique integers between 1 and n+1. For...
P1 Consider an array of length n that contains unique integers between 1 and n+1. For example, an array A1 of length 5 might contain the integers 3, 6, 5, 1, and 4. In an array of this form, one number between 1 and n+1 will be missing. In A1, the integer 2 is missing from the array. As another example, consider the array A2 that contain the integers 4, 7, 5, 2, 6, and 1. A2 has size 6,...
Quicksort (underline the pivot at each step) 19, 22, 8, 90, 13
Quicksort (underline the pivot at each step) 19, 22, 8, 90, 13
If we change the randomized QUICKSORT algorithm so that it repeatedly randomly selects a pivot and...
If we change the randomized QUICKSORT algorithm so that it repeatedly randomly selects a pivot and runs PARTITION until it finds a good pivot, and suppose we keep track of the pivots used so far so we never use one twice for the same array, what is the worst case cost of the algorithm? Give a recurrence for this and its asymptotic solution (you can use the master method.)
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT