第四章 减治法
习题4.5 13. 假设需要在一个n*n的矩阵中搜索一个给定数字,该矩阵每行每列都按升序排列。你能为这个问题设计一个O(n)算法吗?
Compare the search number with the last element in the first row. If they match, stop. If the search number is smaller than the matrix element, the former can’t be in the last column of the matrix, whose elements can be eliminated form the search. If the search number is larger than the last element in the first row, the former can’t be in the first row of the matrix, whose elements can be eliminated from the search. Repeat this step for the smaller matrix until either a match is found or the remaining matrix shrinks to the empty one. Since on each iteration the algorithm eliminates one row or one column of the matrix form a further consideration, its time efficiency class is O(n).
译:把要搜索的数字和第一行最后一个元素比较。如果它们相等,停止。如果要搜索的数字比这个矩阵元素小,那么要搜索的数字不可能在矩阵的最后一列,这些元素可以从搜索中排除。如果要搜索的数字比第一行最后一个元素大,那么要搜索的数字不可能在矩阵的第一行,这些元素可以从搜索中排除。对剩下的较小的矩阵重复这个步骤&