


Array search algorithm with row elements increasing from small to large and column elements increasing from small to large
Question: 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.
Test points: This question is mainly about making good use of the two conditions given, row increment and column increment, to exclude data that is definitely inappropriate, and to reduce the data to be traversed as much as possible.
Array examples are as follows:
1 | 2 | 8 | 9 |
2 | 4 | 9 | 12 |
4 | 7 | 10 | 13 |
6 | 8 | 11 | 15 |
When solving a complex problem, the most effective way is to start with the analysis from the specific problem.
It can be seen from observation that
1. If the beginning of the column is larger than the number you want to find, then the number you want to find cannot be in that column, and you can directly prune that column;
The results are as follows:
1 | 2 |
2 | 4 |
4 | 7 |
6 | 8 |
2. After passing the pruning column, you can Found that the number at the end of the line If it is less than the number you are looking for, then the number you are looking for is definitely not in that row;
The result is as follows:
4 | 7 |
6 | 8 |
3. Like this Just cut the data into the smallest possible amount, and then traverse and search the data. That's it.
The code is as follows:
<?php /* $data 数组 $number 查找的数 $rows 数组的行数 $columns 数组的列数 */ function inArray($data,$number,$rows,$columns) { $row=0; $column=$columns-1; $first=true; while($row<$rows&&$column>=0) { if($data[$row][$column]>$number&&$first) { $column--; //echo $column.','; } if($data[$row][$column]<$number) { $first=false; $row++; //echo $row.','; //如果查找的数大于数组中的所有元素,那么就遍历完所有的行后退出 //continue是防止这种情况的出现,会和第四个条件冲突 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(1,2,8,9),array(2,4,9,12),array(4,7,10,13),array(6,8,11,15)); var_dump(inArray($a,7,4,4)); var_dump(inArray($a,101,4,4));
Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces the array search algorithm in which the row elements increase from small to large and the column elements increase from small to large, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

The data folder contains system and program data, such as software settings and installation packages. Each folder in the Data folder represents a different type of data storage folder, regardless of whether the Data file refers to the file name Data or the extension. Named data, they are all data files customized by the system or program. Data is a backup file for data storage. Generally, it can be opened with meidaplayer, notepad or word.

In PHP programming, array is a very important data structure that can handle large amounts of data easily. PHP provides many array-related functions, array_fill() is one of them. This article will introduce in detail the usage of the array_fill() function, as well as some tips in practical applications. 1. Overview of the array_fill() function The function of the array_fill() function is to create an array of a specified length and composed of the same values. Specifically, the syntax of this function is

Java is a very powerful programming language that is widely used in various development fields. However, during Java programming, developers often encounter ArrayIndexOutOfBoundsException exceptions. So, what are the common causes of this anomaly? ArrayIndexOutOfBoundsException is a common runtime exception in Java. It means that when accessing data, the array subscript exceeds the range of the array. Common reasons include

In PHP programming, array is a frequently used data type. There are also quite a few array operation functions, including the array_change_key_case() function. This function can convert the case of key names in the array to facilitate our data processing. This article will introduce how to use the array_change_key_case() function in PHP. 1. Function syntax and parameters array_change_ke

The differences are: 1. xdata usually refers to independent variables, while data refers to the entire data set; 2. xdata is mainly used to establish data analysis models, while data is used for data analysis and statistics; 3. xdata is usually used for regression Analysis, variance analysis, predictive modeling, data can be analyzed using various statistical methods; 4. xdata usually requires data preprocessing, and data can contain complete original data.
