Search for the existence of a number in a two-dimensional array increasing by row and column
Unique requirement: Two-dimensional arrays increase progressively by Column
Clever Use of the characteristics of the Bipartite method, first compared with the element in the upper right corner, greater than row + 1, less than column-1, otherwise return the row and column
# Include <iostream> using namespace std; # define M 3 void array_bsearch (int array [] [M], int value) {int I = 0; // line int j = M-1; // column while (I <sizeof (array)/M) while (j> = 0) {if (value> array [I] [j]) {I + = 1;} else if (value <array [I] [j]) {j-= 1 ;} else {cout <I <"" <j <endl; // return ;}} cout <"value does not exist" <endl ;} void main (void) {int array [] [M] = {1, 2, 3}, {4, 5, 6}, {7, 8, 9 }}; array_bsearch (array, 1 );}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.