AP计算机教程11-8:中等多选题
0:00
Which of the following correctly shows the iterations of an ascending (from left to right) insertion sort on an array with the following elements: {7,3,8,5,2}?
插入排序,最左侧的元素变为升序,每轮增加一位。
1
What is printed when the following main method is executed?
public class Searcher
{
private int[] arr = {1,3,5,8,9};
public int mystery(int low, int high, int num)
{
int mid = (low + high) / 2;
if (low > high) {
return -1; }
else if (arr[mid] < num) {
return mystery(mid + 1, high, num); }
else if (arr[mid] > num) {
return mystery(low, mid - 1, num); }
else
return mid;
}
public static void main(String[] args)
{
Searcher s = new Searcher();
System.out.println(s.mystery(0,4,3));
}
}
折半查找,
3的索引为1。3
Which of the following correctly shows the iterations of an ascending (from left to right) selection sort on an array with the following elements: {10, 6, 3, 2, 8}?
选择排序,将最小值交换到左侧,每轮增加一位。
4
Which of the following could be used to replace /* missing code */ in the code so that the method always sorts the array elem in ascending order?
public class Searcher
{
public static void sort(int[] elem)
{
for (int j = 0; j < elem.length - 1; j++)
{
int minIndex = j;
for (/* missing code */)
{
if (elem [k] < elem [minIndex])
{
minIndex = k;
}
}
int temp = elem[j];
elem[j] = elem[minIndex];
elem[minIndex] = temp;
}
}
public static void main(String[] args)
{
int[] nums = {28, -3, 2, 14, 30};
Searcher.sort(nums);
}
}
选择排序,内层循环从外层循环当前位置加一开始,遍历剩余的数组元素。
2
0 条评论