二维有序数组(上下有序,左右有序)
1 | 2 | 8 | 9 |
2 | 4 | 9 | 12 |
4 | 7 | 10 | 13 |
6 | 8 | 11 | 15 |
中查找7
bool Find(int *matrix, int rows,int columns,int key){ bool found = false; if (matrix != NULL && rows > 0 && columns > 0) { int row = 0; int column = columns-1; while (row < rows && column < columns) { if (matrix[row * columns + column] == key) { found = true ; break; } else if(matrix[row * columns + column] > key) --column; else ++row; } } return found; } int main(){ int num[4][4]= {1,2,8,9,2,4,9,12,4,7,10,13,6,8,11,15}; cout<<Find(num[0],4,4,17)<<endl; system("pause"); return 0; }
原文:http://blog.csdn.net/h_wlyfw/article/details/20736389