Young's matrix search

Source: Internet
Author: User

1. Brief Introduction

In the young's matrix, each row of elements increases progressively, and each column of elements increases progressively. That is, a [I] [j] <a [I + 1] [j] And a [I] [j] <a [I] [j + 1]. To find the position of a numeric element in such a matrix, the complexity can reach O (M + N), where M is the length of the matrix row, and N is the length of the matrix column.

2. Principles

Recursively run from the lower left corner of the matrix or the upper right corner of the matrix, take the lower left corner as an example, value is the value to be searched, (I, j) is the position in the current matrix, the initial is (M-1, 0 ).
If the matrix range is exceeded, it indicates that such an element does not exist.-1 and-1 are returned.
Otherwise, if the value of the current position is greater than value, it means to move the location, so that the value is reduced, that is, recursion makes I = I-1; if the value of the current location is less than value, it means to move the location, increase the value, that is, recursion causes j = j + 1. If it is equal to value, return the current position I, j.

3. Code

# Include <iostream>
Using namespace std;

# Define M 5
# Define N 4
Int array [M] [N] = {1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,18, 19,20 };

Void find_from_left_bottom (int * a, int I, int j, int m, int n, int value, int & x, int & y ){
If (I <0 | j> = n)
X = y =-1;
Else {
If (* (a + I * n + j) <value)
Find_from_left_bottom (a, I, j + 1, m, n, value, x, y );
Else if (* (a + I * n + j)> value)
Find_from_left_bottom (a, I-1, j, m, n, value, x, y );
Else
X = I, y = j;
}
}
Int main (){
Int X, Y;
Int value = 12;
Find_from_left_bottom (array [0], M-1, 0, M, N, value, x, y );
Cout <x <"" <Y <Endl;
Cout <array [x] [Y] <Endl;
System ("pause ");
Return 0;
}

4. Remarks

When I first encountered this question, I had to look at the matrix in an oblique way and tried to use the binary method. The result was unsuccessful. I still want to explain this method to me.
In addition, when the two-dimensional array is passed, the int * will be passed. After the function is passed, the structure information of the array will be lost, and the common int * instead of the row pointer will be obtained, you can safely and boldly use * (a + I * n + J). Note that N is the length corresponding to the J subscript.

5. Reference

Young's matrix algorithm and thinking http://www.cublog.cn/u3/119410/showart_2348130.html

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.