Diagonal ordered two-dimensional array search

Source: Internet
Author: User

The values of each row in a two-dimensional array increase progressively from left to right. The values of elements increase progressively from top to bottom. How can I quickly determine whether an element value is not in an array?

You can quickly narrow down the range by comparing the data in the upper-right or lower-left corner with the given data.

Therefore:

# Include <stdio. h> # include <stdlib. h> // from the top right corner of int find1 (int data [4] [4], int rows, int cols, int value) {int row, col; if (data! = NULL & rows> 0 & cols> 0) {row = 0; col = cols-1; while (row <rows & col> = 0) {if (data [row] [col] = value) return 1; else if (data [row] [col]> value) col --; elserow ++ ;}} return 0;} // int find2 (int data [4] [4], int rows, int cols, int value) {int row, col; if (data! = NULL & rows> 0 & cols> 0) {row = rows-1; col = 0; while (col <cols & row> = 0) {if (data [row] [col] = value) return 1; else if (data [row] [col]> value) row --; elsecol ++ ;}} return 0;} void main () {int data [4] [4] =, 11,15 }}; if (find1 (data, 4, 4, 11) printf ("11 is in the table \ n "); elseprintf ("11 is not in the table \ n"); if (find1 (data, 4, 4, 10) printf ("10 is in the table \ n "); elseprintf ("10 is not in the table \ n"); if (find1 (data, 4, 4, 5) printf ("5 is in the table \ n "); elseprintf ("5 is not in the table \ n"); if (find1 (data, 4, 4, 3) printf ("3 is in the table \ n "); elseprintf ("3 is not in the table \ n ");}


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.