Data Structures & Algorithms Multiple Choice Questions on “Pigeonhole Sort”.
1. How many comparisons will be made to sort the array arr={1,5,3,8,2} using pigeonhole sort? Answer: d 2. Which of the following is a non-comparison sort? Answer: d 3. In which of the following case pigeonhole sort is most efficient? 4. What is the space complexity of pigeonhole sort (k=range of input)? 5. The auxiliary array used in pigeonhole sorting is called ______________ Answer: d 6. Pigeonhole sort is a stable sorting algorithm. Answer: a 7. Pigeonhole sort is an in place sorting algorithm. Answer: b 8. What is the average time complexity of pigeonhole sort (k=range of input)? Answer: b 9. The complexity of which of the following sorting algorithms remains to be the same in its best, average and worst case? 10. Choose the correct statement from the following. Answer: b 11. What is the advantage of pigeonhole sort over merge sort? Answer: a 12. Which of the following function represents pigeonhole sort correctly? b) c) d) Answer: a 13. Which of the following algorithm takes linear time for sorting? Answer: a
a) 5
b) 7
c) 9
d) 0
Clarification: As pigeonhole sort is an example of a non-comparison sort so it is able to sort an array without making any comparison. So 0 comparisons are required.
a) heap sort
b) quick sort
c) merge sort
d) pigeonhole sort
Clarification: Heap sort, merge sort and quick sort are examples of comparison sort as it needs to compare array elements in order to sort an array. Whereas pigeonhole sort is a non-comparison based sort.
a) when range of input is less than number of elements
b) when range of input is more than number of elements
c) when range of input is comparable to the number of elements
d) when the given array is almost sorted
Answer: c
Clarification: Pigeonhole sort is a non-comparison based sort. It is most efficient in the case where the number of elements are comparable to the input range.
a) O(n*k)
b) O(n)
c) O(k)
d) O(n+k)
Answer: d
Clarification: Pigeonhole sort algorithm requires two arrays. The first one is required to store the input elements so its size is n. The second one is the pigeonhole array and has a size equal to range k. Overall space complexity becomes O(n+k).
a) bucket
b) pigeon
c) hole
d) pigeonhole
Clarification: The auxiliary array used in pigeonhole sorting is called pigeonhole. It is used to store every element in its corresponding hole.
a) true
b) false
View Answer
Clarification: Pigeonhole sort is an example of a stable sorting algorithm. It is because the elements with identical values appear in the same order in the output array as they were in the input array.
a) true
b) false
Clarification: Pigeonhole sort requires space of O(n+k). So it does not qualify to be an in place sorting algorithm.
a) O(n)
b) O(n+k)
c) O(n2)
d) O(n*k)
Clarification: Time complexity of pigeonhole sort is O(n+k). It has two loops. One of the loops runs from 0 to range(k) and the other one runs from 0 to n so the time complexity becomes O(n+k).
a) quick sort
b) insertion sort
c) pigeonhole sort
d) bubble sort
Answer: c
Clarification: The time complexity of pigeonhole remains unvaried in all three cases. It is given by O(n+k). But it is efficient only when the number of elements is comparable to the input range.
a) pigeonhole sort is a comparison based sort
b) any comparison based sorting can be made stable
c) quick sort is not a comparison based sort
d) any comparison based sort requires at least O(n2) time
Clarification: Any comparison based sorting technique can be made stable by considering the position as criteria while making comparisons. Pigeonhole sort is a stable sort.
a) pigeonhole sort has lesser time complexity when range is comparable to number of input elements
b) pigeonhole sort has lesser space complexity
c) counting sort is not a comparison based sorting technique
d) pigeonhole sort is adaptive
Clarification: Pigeonhole sort is efficient in the cases where the range is comparable to a number of input elements as it performs sorting in linear time. Whereas merge sort takes O(n log n) in any case.
a)void Sorting(int arr[], int n)
{
int minimum = arr[0], maximum = arr[0];
for (int i = 1; i < n; i++)
{
if (arr[i] < minimum)
minimum = arr[i];
if (arr[i] > maximum)
maximum = arr[i];
}
int r = maximum - minimum + 1;
vector<int> p_holes[r];
for (int i = 0; i < n; i++)
p_holes[arr[i]-minimum].push_back(arr[i]);
int ind = 0;
for (int i = 0; i < r; i++)
{
vector<int>::iterator it;
for (it = p_holes[i].begin(); it != p_holes[i].end(); ++it)
arr[ind++] = *it;
}
}
void Sorting(int arr[], int n)
{
int minimum = arr[0], maximum = arr[0];
for (int i = 1; i < n; i++)
{
if (arr[i] < minimum)
minimum = arr[i];
if (arr[i] > maximum)
maximum = arr[i];
}
int r = maximum - minimum + 1;
vector<int> p_holes[n];
for (int i = 0; i < n; i++)
p_holes[arr[i]-minimum].push_back(arr[i]);
int ind = 0;
for (int i = 0; i < r; i++)
{
vector<int>::iterator it;
for (it = p_holes[i].begin(); it != p_holes[i].end(); ++it)
arr[ind++] = *it;
}
}
void Sorting(int arr[], int n)
{
int minimum = arr[0], maximum = arr[0];
for (int i = 1; i < n; i++)
{
if (arr[i] < minimum)
minimum = arr[i];
if (arr[i] > maximum)
maximum = arr[i];
}
int r = maximum - minimum + 1;
vector<int> p_holes[r];
for (int i = 0; i < n; i++)
p_holes[arr[i]-minimum].push_back(arr[i]);
int ind = 0;
for (int i = 0; i < n; i++)
{
vector<int>::iterator it;
for (it = p_holes[i].begin(); it != p_holes[i].end(); ++it)
arr[ind++] = *it;
}
}
void Sorting(int arr[], int n)
{
int minimum = arr[0], maximum = arr[0];
for (int i = 1; i < n; i++)
{
if (arr[i] < minimum)
minimum = arr[i];
if (arr[i] > maximum)
maximum = arr[i];
}
int r = maximum - minimum + 1;
vector<int> p_holes[n];
for (int i = 0; i < n; i++)
p_holes[arr[i]-minimum].push_back(arr[i]);
int ind = 0;
for (int i = 0; i < n; i++)
{
vector<int>::iterator it;
for (it = p_holes[i].begin(); it != p_holes[i].end(); ++it)
arr[ind++] = *it;
}
}
Clarification: In pigeonhole sort, we first find the maximum and minimum elements. Then we place the elements in their corresponding holes and then finally put them back into the original array. This sorts the given input.
a) pigeonhole sort
b) heap sort
c) comb sort
d) cycle sort
Clarification: Pigeonhole sort has a linear time complexity of O(n+k). Whereas all other given options have a non linear time complexity. But it should be noted that pigeonhole sort may not be efficient in every case.