java分析算法最佳、最差和平均情况
我想知道find and replaceAll方法和增长函数的最佳、最差和平均情况是什么,增长函数基本上是在以下代码中数组大小大于零的每种情况下执行的语句数
/**
* Return index where value is found in array or -1 if not found.
* @param array ints where value may be found
* @param value int that may be in array
* @return index where value is found or -1 if not found
*/
public static int find(int[] array, int value) {
for (int i = 0; i < array.length; i++) {
if (array[i] == value) {
return i;
}
}
return -1;
}
/**
* Replace all occurrences of oldValue with newValue in array.
* @param array ints where oldValue may be found
* @param oldValue value to replace
* @param newValue new value
*/
public static void replaceAll(int[] array, int oldValue, int newValue) {
int index = find(array, oldValue);
while (index > -1) {
array[index] = newValue;
index = find(array, oldValue);
}
}
共 (0) 个答案