Topic 10: Find in a sorted two-dimensional array

Source: Internet
Author: User

The ordered two-dimensional array is this: the elements are incremented in each row, and the elements in each column are incremented, for example:
11 34 35) 47 51
13 37 40) 52 61
19 42 50) 79 80
Given a value, determine whether it is in such a sorted two-dimensional array.
First, the first to generate test data, the idea is as follows: 1 first select a given input in ascending order. 2) constructs a two-dimensional array, looking for the first element in the array as the starting point, to determine the largest square area (its width is either the same as the original array of rows or the same number of columns as the original array). 3) According to the rules, in this square, each diagonal element is not less than the other elements contained in the square area from the beginning of the array to the element, so when the two-dimensional array is filled, each diagonal element that starts after the first diagonal element, The element before which the diagonal element row (column) is filled with the preceding element, and then populated with the preceding element of the same column (row) as the diagonal elements, and finally in the padding diagonal element. The elements of each location are simply extracted from the sorted one-dimensional array in order. 4) If the two-dimensional array row (column) is larger than the column (row), then the remaining unfilled row (column) is filled in one row (column), and each element is extracted from an array of sorted rows at a time.
In order to facilitate testing, the input array is arranged well:

#include "stdafx.h"#include <iostream>usingnamespacestd;int* CreateTestData(int Row,int Col){    int EleCount = Row*Col;    intnewint[EleCount];    for (int0; i < EleCount; ++i)    {        11;    }    return TestData;}

The following code is written according to the above ideas:

int* Createsorted2darray (intInputarray[],intInputcount,intRow,intCol) {if(Row*col! = Inputcount)returnNULL;int* res = newint[Inputcount];intsquarewidth = min (row,col);int Index=0; for(inti =0; i < squarewidth; ++i) { for(intj =0; J < I; ++J) {Res[i*col+ j] = inputarray[Index++]; } for(intj =0; J < I; ++J) {Res[j*col+ i] = inputarray[Index++]; } res[i*col+ i] = inputarray[Index++]; }if(Squarewidth = = Row) { for(intj = squarewidth; J < Col; ++J) { for(inti =0; i < Row; ++i) {Res[i*col+ j] = inputarray[Index++]; }        }    }Else{ for(inti = squarewidth; i < Row; ++i) { for(intj =0; J < Col; ++J) {Res[i*col+ j] = inputarray[Index++]; }        }    }returnRes;}

Display two-dimensional array elements:

void Display2DArray(int InputArray[],int Row,int Col){    for (int0; i < Row; ++i)    {        for (int0; j < Col; ++j)        {            cout<<InputArray[i*Col + j]<<" ";        }        cout<<endl;    }}

Test function:

int_tmain (intARGC, _tchar* argv[]) {cout<<"============row = 8,col = 5=============="<<endl;int m=8, n =5;int* TestData1 = Createtestdata (m, n);int* Sorted2darray1 = Createsorted2darray (TestData1,m*n,m, n); Display2darray (Sorted2darray1,m, n); cout<<"============row = 5,col = 8=============="<<endl;m=5, n =8;int* TestData2 = Createtestdata (m, n);int* Sorted2darray2 = Createsorted2darray (TestData2,m*n,m, n); Display2darray (Sorted2darray2,m, n);return 0;}

Program run:

When looking, starting from the upper-right corner of the two-dimensional array, if the element v is removed equals the element to be found, then the lookup succeeds, returns if V

BOOLIscontained (Const intSorted2darray[],Const intRow,Const intCol,Const int value){inti =0, j = Col-1; while(I < Row && J >=0)    {intv = sorted2darray[i*col + j];if(v = =value)return true;Else if(V <value)        {if(I < Row) ++i; }Else{if(J >0)--j; }    }return false;}

Test function:

int_tmain (intARGC, _tchar* argv[]) {cout<<"============row = 8,col = 5=============="<<endl;intm =8, n =5;int* TestData1 = Createtestdata (m,n);int* Sorted2darray1 = Createsorted2darray (testdata1,m*n,m,n); Display2darray (Sorted2darray1,m,n);cout<<"============row = 5,col = 8=============="<<endl; m =5, n =8;int* TestData2 = Createtestdata (m,n);int* Sorted2darray2 = Createsorted2darray (testdata2,m*n,m,n); Display2darray (Sorted2darray2,m,n);cout<<iscontained (Sorted2darray2,m,n, -) <<endl;cout<<iscontained (Sorted2darray1,n,m, $) <<endl;return 0;}

Program run:

Topic 10: Find in a sorted two-dimensional array

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.