# Search in two-dimensional array

Source: Internet
Author: User

Question: In a two-dimensional array, each row is sorted in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Complete a function, input a two-dimensional array and an integer to determine whether the array contains the integer.

Example: The following is a two-dimensional array that meets the question requirements. If the number is 7 in this array, true is returned. If the number is 5, false is returned because the number is not included.

Algorithm Analysis: first, select the number in the upper-right corner of the array. If the number is equal to the number to be searched, the search process ends. If the number is greater than the number to be searched, the column where the number is located is removed. If the number is smaller than the number to be searched, remove the row where the number is located. In this way, the search range can be reduced until the number to be searched is found or the search range is empty.

For example, the specific search process is as follows:

First, we select the number 9 in the upper-right corner of the array. Because 9> 7 and 9 are the first (minimum) number in column 4th, 7 cannot appear in the column where number 9 is located. Therefore, we remove this column from the region to be considered, and then we only need to analyze the remaining three columns. In the remaining matrix, the number in the upper right corner is 8. Similarly, 8> 7. Therefore, we can remove the column where 8 is located. Next, you only need to analyze the remaining two columns.

In the array composed of the remaining two columns, number 2 is located in the upper right corner of the array. 2 <7, the 7 to be searched may be on the Right of 2, or under 2. In the previous step, we found that all columns on the Right of 2 have been removed, that is, 7 cannot appear on the Right of 2, so 7 can only appear under 2. Therefore, we can remove the row where number 2 is located and analyze only the remaining three rows and two columns of numbers. In the remaining number, number 4 is located in the upper right corner. Like the preceding one, we remove the row where number 4 is located, and the last two rows and two columns of numbers are left.

In the remaining two rows, two columns, and four numbers, the number 7 in the upper right corner is the number 7 we are looking for, so the search process is complete.

Search process diagram:

Note: The area of the Shadow background in the matrix is the range of the next search.

The Code is as follows:

`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;}elseif(matrix[row * columns + column] > number)-- column;else++ row;}}return found;}`

In addition, you can also start searching from the number in the lower left corner. process: first select the number in the lower left corner of the array. If the number is equal to the number to be searched, the search process ends. If the number is greater than the number to be searched, the row where the number is located is removed. If the number is smaller than the number to be searched, remove the column where the number is located. In this way, the search range can be reduced until the number to be searched is found or the search range is empty.

The Code is as follows:

`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;}elseif(matrix[row * columns + column] < number)++ column;else-- row;}return found;}`

Note: The search range cannot be reduced from the upper left or lower right corner, so the search cannot start from the number in the upper left or lower right corner.

Test cases:

● The two-dimensional array contains the number to be searched (the number to be searched is the maximum and minimum values in the array, and the number to be searched is between the maximum and minimum values in the array );

● No number found in the two-dimensional array (the number to be searched is greater than the maximum value in the array, that is, the number in the lower right corner. the number to be searched is smaller than the minimum value in the array, that is, the number in the upper left corner, the number to be searched is between the maximum and minimum values of the array, but this number is not in the array );

● Special input test (input NULL pointer ).

Complete search code example:

`#include<stdio.h>#define bool int#define true 1#define false 0bool 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;}elseif(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");elseprintf("no find number\n");return 0;}`

Result:

PS: 1. the parameter transfer types of two-dimensional arrays and pointers should be consistent;

2. Representation of two-dimensional array elements, stored by row;

3. Condition judgment and changes of rows and columns.

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.