The instruction count of each instruction in the algorithm is indicated at the right of the statement after the //. You should replace each of the question marks(?) by the corresponding instruction count in order to complete the analysis. The total instruction count is obtained by adding all the instruction counts. Note that in this exercise, the linear search algorithm is analyzed in a case where the item to be searched is assumed to be found in the first cell of the array (i.e. Best Case Analysis). int linearSearch(item, array) { // best case instruction count int index = 0; // ? while (index < array.length) { // ? if (array[index] == item) { // ? return index; // ? } index++; //? } return -1; // ? } // ------ // T(n) = ? where n is the array size
The instruction count of each instruction in the
analysis. The total instruction count is obtained by adding all the instruction counts. Note that in this exercise, the linear search algorithm is analyzed in a case where the item to be searched is assumed to be found in the first cell of the array (i.e. Best Case Analysis).
int linearSearch(item, array) { // best case instruction count
int index = 0; // ?
while (index < array.length) { // ?
if (array[index] == item) { // ?
return index; // ?
}
index++; //?
}
return -1; // ?
} // ------
// T(n) = ? where n is the array size
Step by step
Solved in 2 steps