AP计算机教程11-7:简单多选题
0:00
What would the following code return from mystery([90, -30, 50], 50)
?
public static int mystery(int[] elements, int target) { for (int j = 0; j < elements.length; j++) { if (elements[j] == target) { return j; } } return -1; }
顺序查找,
50
的索引是2
。4
What would the following code return from mystery([90, -30, 50], -20)
?
public static int mystery(int[] elements, int target) { for (int j = 0; j < elements.length; j++) { if (elements[j] == target) { return j; } } return -1; }
顺序查找,
-20
不在数组中,返回-1
。1
Consider the binarySearch
method below. How many times would the while loop execute if you first do int[] arr = {2, 10, 23, 31, 55, 86}
and then call binarySearch(arr,2)
?
public static int binarySearch(int[] elements, int target) { int left = 0; int right = elements.length - 1; while (left <= right) { int middle = (left + right) / 2; if (target < elements[middle]) { right = middle - 1; } else if (target > elements[middle]) { left = middle + 1; } else { return middle; } } return -1; }
查找23
、2
,循环两轮。2
Which sort contains a recursive call?
归并排序利用了递归。
3
Under what condition will an ascending insertion sort execute the slowest?
当数组降序排列时,插入排序的内层循环每次都要完整执行,此时算法耗时最久。
2
0 条评论