二维数组中的查找

题目:在一个二维数组中,每一行都依照从左到右递增的顺序排序,每一列都依照从上到下递增的顺序排序。请完毕一个函数,输入这种一个二维数组和一个整数,推断数组中是否含有该整数。

演示样例:以下是一个满足题目要求的二维数组。假设在这个数组中查找数字7,则返回true;假设查找数字5,因为不含该数字,返回false。


算法分析:首先选取数组中右上角的数字。假设该数字等于要查找的数字,查找过程结束;假设该数字大于要查找的数字,剔除这个数字所在的列;假设该数字小于要查找的数字,剔除这个数字所在的行。这样,每一步都能够缩小查找范围,直到找到要查找的数字,或者查找范围为空。

针对演示样例,详细查找步骤例如以下:

首先我们选取数组右上角的数字9.因为9>7,而且9还是第4列的第一个(也是最小的)数字,因此7不可能出如今数字9所在的列。于是我们把这一列从须要考虑的区域内剔除,之后仅仅须要分析剩下的3列。在剩下的矩阵中,位于右上角的数字是8,相同地,8>7,因此8所在的列我们也能够剔除。接下来仅仅须要分析剩下的两列就可以。

在由剩余的两列组成的数组中,数字2位于数组的右上角。2<7,那么要查找的7可能在2的右边,也有可能在2的下边。在前面的步骤中,我们已经发现2右边的列都已经被剔除了,也就是说7不可能出如今2的右边,因此7仅仅有可能出如今2的下边。于是我们把数字2所在的行也剔除,仅仅分析剩下的三行两列数字。在剩下的数字中,数字4位于右上角,和前面一样,我们把数字4所在的行也剔除,最后剩下两行两列数字。

在剩下的两行两列4个数字中,位于右上角的刚好就是我们要查找的数字7,于是查找过程就能够结束了。

查找过程图解:


注:矩阵中加阴影背景的区域是下一步查找的范围。

代码例如以下:

bool find_number(int* matrix, int rows, int columns, int number)
{
	bool found = false;
	if(matrix != NULL && rows > 0 && columns > 0)
	{
		int row = 0;
		int column = columns - 1;
		while(row < rows && column >= 0)
		{
			if(matrix[row * columns + column] == number)
			{
				printf("row = %d, column = %d\n", row, column);
				return found = true;
				break;
			}
			else
				if(matrix[row * columns + column] > number)
					-- column;
				else
					++ row;
		}
	}
	return found;
}

另外,也能够从左下角的数字開始查找,过程:首先选取数组中左下角的数字。假设该数字等于要查找的数字,查找过程结束;假设该数字大于要查找的数字,剔除这个数字所在的行;假设该数字小于要查找的数字,剔除这个数字所在的列。这样,每一步都能够缩小查找范围,直到找到要查找的数字,或者查找范围为空。

代码例如以下:

bool find_number(int* matrix, int rows, int columns, int number)
{
	bool found = false;
	int row = rows - 1;
	int column = 0;
	while(matrix != NULL && row >= 0 && column < columns)
	{
			if(matrix[row * columns + column] == number)
			{
				printf("row = %d, column = %d\n", row, column);
				return found = true;
				break;
			}
			else
				if(matrix[row * columns + column] < number)
					++ column;
				else
					-- row;
	}
	return found;
}

注意:从左上角或右下角開始查找都不能缩小查找范围,所以不能从左上角或右下角的数字開始查找。

測试用例:

●  二维数组中包括查找的数字(查找的数字是数组中的最大值和最小值,查找的数字介于数组中的最大值和最小值之间);

●  二维数组中没有查找的数字(查找的数字大于数组中的最大值,即右下角的数字,查找的数字小于数组中的最小值,即左上角的数字,查找的数字在数组的最大值和最小值之间,但数组中没有这个数字);

●  特殊输入測试(输入空指针)。

完整查找代码演示样例:

#include<stdio.h>

#define bool int
#define true 1
#define false 0

bool find_number(int* matrix, int rows, int columns, int number)
{
	bool found = false;
	if(matrix != NULL && rows > 0 && columns > 0)
	{
		int row = 0;
		int column = columns - 1;
		while(row < rows && column >= 0)
		{
			if(matrix[row * columns + column] == number)
			{
				printf("row = %d, column = %d\n", row, column);
				return found = true;
				break;
			}
			else
				if(matrix[row * columns + column] > number)
					-- column;
				else
					++ row;
		}
	}
	return found;
}

int main()
{
	int matrix[][4]={{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
	bool result = find_number((int*)matrix,4,4,7);
	if(result == true)
		printf("number existed\n");
	else
		printf("no find number\n");
	return 0;
}

结果:


ps:1、二维数组和指针的參数传递类型应该一致;

         2、二维数组元素的表示,按行存放;

         3、条件的推断,行、列的变化。

posted @ 2014-07-30 12:26  hrhguanli  阅读(232)  评论(0编辑  收藏  举报