Week-11 Assignment - July - 2024
Week-11 Assignment - July - 2024
QUESTION 1:
Which sorting algorithm is implemented in the following code:
// Find the index of the minimum element in the unsorted part of the array
for (int j = i + 1; j < len; j++) {
// Compare current minIndex value with the next element
if (arr[minIndex].compareTo(arr[j]) > 0) {
minIndex = j; // Update minIndex if a smaller element is found
}
}
// Swap the found minimum element with the first element of the unsorted part
T temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
// Method to print the array elements
public static <T> void printArray(T[] arr, int len) {
for (int i = 0; i < len; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
a. Heap Sort
b. Merge Sort
c. Insertion Sort
d. Selection Sort
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Detailed Solution:
Selection sort is implemented in the provided code. For a detailed explanation, please refer to slides 90-92
from Week 11, Lec 51-55.
___________________________________________________________________________
QUESTION 2:
Given the following code snippet:
Assume the MinHeap class has a standard implementation of a MinHeap. What will be the correct
output of the program?
a.
b.
3 5 6 9 10 17 19 22 84
c.
d.
Correct Answer: a
Detailed Solution:
Detailed Solution:
Counting Sort is a non-comparison-based sorting algorithm that works well for integers or small-range
inputs.
___________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
What is the time complexity of Quick Sort in its average case?
a. O(n)
b. O(n log n)
c. O(n^2)
d. O(log n)
Correct Answer: b. O(n log n)
Detailed Solution:
Quick Sort has an average-case time complexity of O(n log n).
___________________________________________________________________________
QUESTION 5:
Choose the best option to fill in blank spaces given in the BubbleSort algorithm below
For i = 1 to _____(1)_____ do
For j = 1 to _____(2)_____ do
If A[j] _____(3)_____ A[j+1] then
Swap(A[j], A[j+1])
EndIf
j=j+1
EndFor
EndFor
Stop
a.
1. n-1
2. n-1
3. >
b.
1. n-1
2. n-1
3. <
c.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
1. n-1
2. n-i
3. <
d.
1. n-1
2. n-i
3. >
Correct Answer: d
Detailed Solution:
Blank (1): n-1
○ The outer loop runs from 1 to n-1 because each pass through the array sorts the
largest unsorted element into its final position.
2. Blank (2): n-i
○ The inner loop runs from 1 to n-i to avoid comparing already sorted elements.
3. Blank (3): >
○ Bubble Sort swaps elements if the current element is greater than the next
element, so A[j] > A[j+1] triggers the swap.
1. For i = 1 to n-1 do
2. For j = 1 to n-i do
3. If (A[j] > A[j+1]) then
4. Swap(A[j], A[j+1])
5. EndIf
6. j=j+1
7. EndFor
8. EndFor
9. Stop
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
___________________________________________________________________________
QUESTION 6:
Which library in Java contains the sort() function?
a. java.io
b. java.util
c. java.applet
d. java.swing
Correct Answer: b. java.util
Detailed Solution:
sort() function was present in java.util library.
___________________________________________________________________________
QUESTION 7:
Consider the code snippet given below.
import java.util.Arrays;
public class SortingExample {
public static void main(String[] args) {
int[] arr = {5, 2, 9, 1, 5, 6};
System.out.println("Original Array:");
printArray(arr);
________________// Fill in the blank
Detailed Solution:
Arrays.sort(arr) is the right syntax.
___________________________________________________________________________
QUESTION 8:
Consider the code given below.
1. for (i=0;i<10;i++)
2. {
3. for(j=0;j<10;j++)
4. {
5. if(arr[i] < arr[j])
6. {
7. temp = arr[i];
8. arr[i] = arr[j];
9. arr[j] =arr[i];
10. }
11. }
12. }
Correct Answer: d. 9
Detailed Solution:
the correct line of code is arr[j] = temp.
___________________________________________________________________________
QUESTION 9:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
When implementing the Bubble Sort algorithm, what is the time complexity in the worst-
case scenario?
a. O(n)
b. O(n log n)
c. O(n^2)
d. O(log n)
Correct Answer: c. O(n^2)
Detailed Solution:
The worst-case time complexity of the Bubble Sort algorithm is O(n^2), as it involves nested loops to
compare and swap elements.
___________________________________________________________________________
QUESTION 10:
Given an array of 1 million integers with values ranging from -10,000 to 10,000, which
sorting algorithm is most suitable for sorting this array efficiently with the least amount of
memory usage?
a. Quick Sort
b. Merge Sort
c. Bubble Sort
d. Radix Sort
Detailed Solution:
Radix Sort is the most suitable option for sorting a large array of integers when the range of values is
known and relatively small, as it uses a counting sort-based approach for each digit, minimizing memory
usage. In this case, the values range from -10,000 to 10,000, which is a range of 20,001 integers, making
Radix Sort an efficient and memory-efficient choice. Quick Sort and Merge Sort have a higher memory
overhead, and Bubble Sort is not suitable for large datasets due to its time complexity.
___________________________________________________________________________
************END************