Search in two-dimensional array (C language implementation)

Topic description

In a two-dimensional array, each row is sorted in increasing order from left to right, and each column is sorted in increasing order from top to bottom. Please complete a function, input such a two-dimensional array and an integer, and determine whether the array contains the integer.


Think : In each row of this two-dimensional array, the leftmost number is the smallest, and in each column, the topmost number is the smallest; then, we can find a middle value (that is, the rightmost element of the first row , which is also the first element of the last column); let the median value be compared with the integer, if the median value is less than the integer, it means that the values ​​of this row are all less than the integer (that is, the row can be excluded), if the median value is greater than the integer , then all elements of the column are greater than the integer (that is, the column can be excluded).

After excluding a row or column, find the middle value (the value in the upper right corner of the remaining array) again, continue the above comparison rules, know that there is one element left at the end, if the element is not equal to this integer, terminate the loop and return false; otherwise , terminates the loop and returns true.


Code implementation (C language) :

1. First define your own myUtil.h file.

#ifndef _MY_UTIL_H_
#define _MY_UTIL_H_
/*
Customize the myUtil.h file
The Boolean type is defined in the file
		TRUE and FALSE are defined
 */
typedef unsigned char boolean;
#define FALSE '0'
#define TRUE '1'

#endif
2. Write the test test.c file.
#include <stdio.h>

#include "myUtil.h"

boolean FindNum(char **array, int rows, int columns, int num);

boolean FindNum(char **array, int rows, int columns, int num) {
	boolean result = FALSE;
	int row;
	int col;
	int i = 0;
	int j = 0;
	int tag = 0;//tag tag, the tag required when outputting a two-dimensional array in the method
	if(array == NULL || rows < 0 || columns < 0) {
		printf("The input of the array is wrong...\n");
		return result;
	}
	row = 0;
	col = columns - 1;
	
	// printf("Use the passed 2D array parameter to output a 2D array\n");
	// for(i = 0; i < rows*columns; i++) {
		
	// 	printf("%d,", array[i]);
	// 	if(tag == columns-1) {
	// 		tag = 0;
	// 		printf("\n");
	// 	} else {
	// 		tag++;
	// 	}
	// }
	printf("Number to find: %d\n", num);
	while(row < rows && col >= 0) {
		//printf("%d\n", col);
		if(array[row*col + col] == num) {
			result = TRUE;
			break;
		}
		if(array[row*col + col] > num) {
			col--;
		} else {
			row++;
		}
	}
	return result;
}

int main(int argc, char const *argv[])
{
	/* code */
	int i;
	int j;
	boolean result;
	int array[5][5] = {	{1,2,8,9,11},
			        {2,4,9,12,13},
				{4,7,10,13,15},
				{6,8,11,15,18},
				{10,12,14,18,22},};
	int num = 0;
	
	printf("Please enter an integer num:\n");
	scanf("%d", &num);
	result = FindNum(array, 5, 5, num);
	printf("The result is [%s]\n", result == '0' ? "This element does not exist in the array" : "This element exists in the array");
	return 0;
}

3. Test results




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325721376&siteId=291194637