Data Structure Multiple Choice Questions on “Quicksort – 3”.
1. QuickSort can be categorized into which of the following? Answer: b 2. Select the appropriate recursive call for QuickSort.(arr is the array, low is the starting index and high is the ending index of the array, partition returns the pivot element, we will see the code for partition very soon) b) c) d) Answer: a 3. What is the worst case complexity of QuickSort? Answer: d 4. What is a randomized QuickSort? Answer: c 5. Which of the following code performs the partition operation in QuickSort? b) c) d) Answer: b 6. What is the best case complexity of QuickSort? 7. The given array is arr = {2,3,4,1,6}. What are the pivots that are returned as a result of subsequent partitioning? Answer: a 8. What is the average case complexity of QuickSort? Answer: a 9. The given array is arr = {2,6,1}. What are the pivots that are returned as a result of subsequent partitioning? Answer: d 10. Which of the following is not true about QuickSort? Answer: b
a) Brute Force technique
b) Divide and conquer
c) Greedy algorithm
d) Dynamic programming
Clarification: First you divide(partition) the array based on the pivot element and sort accordingly.
a)public static void quickSort(int[] arr, int low, int high)
{
int pivot;
if(high>low)
{
pivot = partition(arr, low, high);
quickSort(arr, low, pivot-1);
quickSort(arr, pivot+1, high);
}
}
public static void quickSort(int[] arr, int low, int high)
{
int pivot;
if(high<low)
{
pivot = partition(arr, low, high);
quickSort(arr, low, pivot-1);
quickSort(arr, pivot+1, high);
}
}
public static void quickSort(int[] arr, int low, int high)
{
int pivot;
if(high>low)
{
pivot = partition(arr, low, high);
quickSort(arr, low, pivot);
quickSort(arr, pivot, high);
}
}
public static void quickSort(int[] arr, int low, int high)
{
int pivot;
if(high>low)
{
pivot = partition(arr, low, high);
quickSort(arr, low, pivot);
quickSort(arr, pivot+2, high);
}
}
Clarification: Based on the pivot returned by the call to partition, recursive calls to quickSort sort the given array.
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
Clarification: When the input array is already sorted.
a) The leftmost element is chosen as the pivot
b) The rightmost element is chosen as the pivot
c) Any element in the array is chosen as the pivot
d) A random number is generated which is used as the pivot
Clarification: QuickSort is randomized by placing the input data in the randomized fashion in the array or by choosing a random element in the array as a pivot.
a)private static int partition(int[] arr, int low, int high)
{
int left, right, pivot_item = arr[low];
left = low;
right = high;
while(left > right)
{
while(arr[left] <= pivot_item)
{
left++;
}
while(arr[right] > pivot_item)
{
right--;
}
if(left < right)
{
swap(arr, left, right);
}
}
arr[low] = arr[right];
arr[right] = pivot_item;
return right;
}
private static int partition(int[] arr, int low, int high)
{
int left, right, pivot_item = arr[low];
left = low;
right = high;
while(left <= right)
{
while(arr[left] <= pivot_item)
{
left++;
}
while(arr[right] > pivot_item)
{
right--;
}
if(left < right)
{
swap(arr, left, right);
}
}
arr[low] = arr[right];
arr[right] = pivot_item;
return right;
}
private static int partition(int[] arr, int low, int high)
{
int left, right, pivot_item = arr[low];
left = low;
right = high;
while(left <= right)
{
while(arr[left] > pivot_item)
{
left++;
}
while(arr[right] <= pivot_item)
{
right--;
}
if(left < right)
{
swap(arr, left, right);
}
}
arr[low] = arr[right];
arr[right] = pivot_item;
return right;
}
private static int partition(int[] arr, int low, int high)
{
int left, right, pivot_item = arr[low];
left = low;
right = high;
while(left > right)
{
while(arr[left] > pivot_item)
{
left++;
}
while(arr[right] <= pivot_item)
{
right--;
}
if(left < right)
{
swap(arr, left, right);
}
}
arr[low] = arr[right];
arr[right] = pivot_item;
return right;
}
Clarification: The array is partitioned such that the elements left to the pivot are lesser than the pivot while the elements right of the pivot are greater than the pivot.
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
Answer: a
Clarification: The array is partitioned into equal halves, using the Divide and Conquer master theorem, the complexity is found to be O(nlogn).
a) 1 and 3
b) 3 and 1
c) 2 and 6
d) 6 and 2
Clarification: The call to partition returns 1 and 3 as the pivot elements.
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
Clarification: The position of partition(split) is unknown, hence all(n) possibilities are considered, the average is found by adding all and dividing by n.
a) 1 and 6
b) 6 and 1
c) 2 and 6
d) 1
Clarification: There is only one pivot with which the array will be sorted, the pivot is 1.
a) in-place algorithm
b) pivot position can be changed
c) adaptive sorting algorithm
d) can be implemented as a stable sort
Clarification: Once a pivot is chosen, its position is finalized in the sorted array, it cannot be modified.