Array search algorithms for increasing row elements from small to large and column elements from small to large

Source: Internet
Author: User
Tags array example
: This article mainly introduces the array search algorithm for increasing row elements from small to large and column elements from small to large. if you are interested in PHP tutorials, refer to it. 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.

Test point: This question mainly aims to take advantage of the given two conditions, row increments and column increments, to exclude unsuitable data and minimize the amount of data to be traversed.

An array example is as follows:

1 2 8 9
2 4 9 12
4 7 10 13
6 8 11 15

The most effective way to solve a complex problem is to analyze the specific problem.

We can see through observation that,

1. if the beginning of a column is greater than the number to be searched, the number to be searched cannot be in that column. you can directly delete that column;

The result is as follows:

1 2
2 4
4 7
6 8

2. after pruning the column, you can find that if the number at the end of the row is smaller than the number to be searched, the number to be searched is certainly not in that row;

The result is as follows:

4 7
6 8

3. in this way, the data is cut into the least possible quantity, and then the data is traversed and searched.

The code is as follows:

 = 0) {if ($ data [$ row] [$ column]> $ number & $ first) {$ column --; // echo $ column. ',';} if ($ data [$ row] [$ column] <$ number) {$ first = false; $ row ++; // echo $ row. ','; // if the number of queries is greater than all elements in the array, traverse all rows and exit. // continue prevents this situation, will conflict with the fourth condition continue;} if ($ data [$ row] [$ column] ==$ number) {return true ;} if ($ data [$ row] [$ column]> $ number &&! $ First) {break ;}}for ($ I = $ row; $ I <$ rows; $ I ++) {for ($ j = 0; $ j <$ column; $ j ++) {if ($ data [$ I] [$ j] ==$ number) {return true ;}} return false ;} $ a = array (,), array )); var_dump (inArray ($ a, 4); var_dump (inArray ($ a, 10, 4 ));

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The preceding section describes the array search algorithms for increasing row elements from small to large, and column elements from small to large, including some content. I hope to help anyone who is interested in the PHP Tutorial.

Contact Us

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.