理想情況下,我希望計數器一旦找到值就停止。當我運行程序時,它會繼續比較矩陣中的其他值。二維陣列排序列表的線性搜索
public static void LinearSearch1(int[][] matrix, int array_size, int target) {
int row, col;
int comparison_counter = 0;
boolean found = false;
while (!found)
{
for (row = 0; row < array_size; row++)
{
for (col = 0; col < array_size; col++)
{
comparison_counter++;
if (matrix[row][col] == target)
{
found = true;
}//end if
}//end inner for
}//end outer for
} //end while
System.out.println(target + " found in " + comparison_counter + " number of comparisons using linear search");
}
這是一個錯字,謝謝! –