The values of each row in a two-dimensional array increase progressively from left to right. The values of elements increase progressively from top to bottom. How can I quickly determine whether an element value is not in an array?
You can quickly narrow down the range by comparing the data in the upper-right or lower-left corner with the given data.
Therefore:
# Include <stdio. h> # include <stdlib. h> // from the top right corner of int find1 (int data [4] [4], int rows, int cols, int value) {int row, col; if (data! = NULL & rows> 0 & cols> 0) {row = 0; col = cols-1; while (row <rows & col> = 0) {if (data [row] [col] = value) return 1; else if (data [row] [col]> value) col --; elserow ++ ;}} return 0;} // int find2 (int data [4] [4], int rows, int cols, int value) {int row, col; if (data! = NULL & rows> 0 & cols> 0) {row = rows-1; col = 0; while (col <cols & row> = 0) {if (data [row] [col] = value) return 1; else if (data [row] [col]> value) row --; elsecol ++ ;}} return 0;} void main () {int data [4] [4] =, 11,15 }}; if (find1 (data, 4, 4, 11) printf ("11 is in the table \ n "); elseprintf ("11 is not in the table \ n"); if (find1 (data, 4, 4, 10) printf ("10 is in the table \ n "); elseprintf ("10 is not in the table \ n"); if (find1 (data, 4, 4, 5) printf ("5 is in the table \ n "); elseprintf ("5 is not in the table \ n"); if (find1 (data, 4, 4, 3) printf ("3 is in the table \ n "); elseprintf ("3 is not in the table \ n ");}