250+ TOP MCQs on Quicksort and Answers

Data Structure Multiple Choice Questions on “Quicksort – 3”.

1. QuickSort can be categorized into which of the following?
a) Brute Force technique
b) Divide and conquer
c) Greedy algorithm
d) Dynamic programming

Answer: b
Clarification: First you divide(partition) the array based on the pivot element and sort accordingly.

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)
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);
	}
}

b)

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);
	}
}

c)

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);
	}
}

d)

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);
	}
}

Answer: a
Clarification: Based on the pivot returned by the call to partition, recursive calls to quickSort sort the given array.

 
 

3. What is the worst case complexity of QuickSort?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)

Answer: d
Clarification: When the input array is already sorted.

4. What is a randomized QuickSort?
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

Answer: c
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.

5. Which of the following code performs the partition operation in QuickSort?
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;
}

b)

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;
}

c)

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;
}

d)

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;
}

Answer: b
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.

6. What is the best case complexity of QuickSort?
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).

7. The given array is arr = {2,3,4,1,6}. What are the pivots that are returned as a result of subsequent partitioning?
a) 1 and 3
b) 3 and 1
c) 2 and 6
d) 6 and 2

Answer: a
Clarification: The call to partition returns 1 and 3 as the pivot elements.

8. What is the average case complexity of QuickSort?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)

Answer: a
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.

9. The given array is arr = {2,6,1}. What are the pivots that are returned as a result of subsequent partitioning?
a) 1 and 6
b) 6 and 1
c) 2 and 6
d) 1

Answer: d
Clarification: There is only one pivot with which the array will be sorted, the pivot is 1.

10. Which of the following is not true about QuickSort?
a) in-place algorithm
b) pivot position can be changed
c) adaptive sorting algorithm
d) can be implemented as a stable sort

Answer: b
Clarification: Once a pivot is chosen, its position is finalized in the sorted array, it cannot be modified.

and Answers.

Leave a Reply

Your email address will not be published. Required fields are marked *