Sorting Algorithms - Study Mode
[#486] Consider the following code snippet, which implements the Shell sort algorithm. shellSort( int elements[], int num_elements , int incrmnts[], int num_incrmnts)
{
int incr, j, k, span, y
for(incr = 0
incr
< num_incrmnts
incr++)
{
span = incrmnts[incr]
data-structure-questions-answers-shell-sort
for( j = span
j < num_elements
j++)
{
k = j
y = elements[j]
while (________ )
{
elements [ k] = elements[k - span]
k = k - span
}
elements[k] = y
}
} Which condition will correctly implement the while loop?
Correct Answer
(D) k >= span && y < elements[k- span]
[#487] What will be the base case in the function of binary search used in the code of binary insertion sort? (high and low are the rightmost and leftmost index of array respectively and item is the element whose correct position is to be determined by the binary search function)
Correct Answer
(C) If(high<=low)
{
If(Item<a[low])
return low
return low+1
}
[#488] Which of the following code fragment puts sorted values in an array using beads correctly?
Correct Answer
(B) for (int i = 0
i < n
i++)
{
int j
for (j = 0
j < max && beads[i * max + j]
j++)
//max is the maximum value element of given array a[]
a[i] = j
}
[#489] Consider the code given below, which runs insertion sort: void insertionSort(int arr[], int array_size)
{
int i, j, value
for (i = 1
i < array_size
i++)
{
value = arr[i]
j = i
while (________ )
{
arr[j] = arr[j − 1]
j = j − 1
}
arr[j] = value
}
} Which condition will correctly implement the while loop?
Correct Answer
(B) (j > 0) && (arr[j - 1] > value)
[#490] What will be the output of the given Java code? import java.util.Arrays
public class SortExample
{
public static void main(String[] args)
{
int[] arr = {10,7,9,5,8,4}
Arrays.sort(arr, 1, 3)
System.out.printf(Arrays.toString(arr))
}
}
Correct Answer
(D) [10,7,9,5,8,4]