每日一题10:在排序的二维数组中查找

来源:互联网 发布:服务器硬件检测软件 编辑:程序博客网 时间:2024/04/29 22:23

排序的二维数组是这样的:在每一行中元素是递增的,在每一列中元素也是递增的,比如:
11 34 35 47 51
13 37 40 52 61
19 42 50 79 80
给定一个值,判断其是否在这样排序的二维数组中。
首先,先来生成测试数据,思路如下:1)先选择一种将给定输入按升序排列。2)构造一个二维数组,寻找该数组中以第一个元素为起点,确定一个最大的正方形区域(其宽要么与原数组的行或与原数组的列数相同)。3)按规则,在这个正方形中,每个对角线元素都不小于从数组起点到这个元素所构成的正方形区域所包含的其他元素,所以在填充这个二维数组时,对从第一个对角线元素开始后的每一个对角线元素,先填充与这个对角线同元素行(列)的之前的元素,然后再填充与这个对角线元素同列(行)的之前的元素,最后在填充对角线元素。每个位置的元素只需顺序从经过排序的一维数组中提取。4)如果二维数组行(列)大于列(行),那么就将剩下未填充的位置一行(列)一行(列)地填充,每个元素还是一次从排序的一位数组中提取。
为了方便测试,输入数组就是排列好的:

#include "stdafx.h"#include <iostream>using namespace std;int* CreateTestData(int Row,int Col){    int EleCount = Row*Col;    int *TestData = new int[EleCount];    for (int i = 0; i < EleCount; ++i)    {        TestData[i] = i + 11;    }    return TestData;}

按上面的思路编写的代码如下:

int* CreateSorted2DArray(int InputArray[],int InputCount,int Row,int Col){    if(Row*Col != InputCount) return NULL;    int* res = new int[InputCount];    int SquareWidth = min(Row,Col);    int index = 0;    for (int i = 0; i < SquareWidth; ++i)    {        for (int j = 0; j < i; ++j)        {            res[i*Col + j] = InputArray[index++];        }        for (int j = 0; j < i; ++j)        {            res[j*Col + i] = InputArray[index++];        }        res[i*Col + i] = InputArray[index++];    }    if(SquareWidth == Row)    {        for (int j = SquareWidth; j < Col; ++j)        {            for (int i = 0; i < Row; ++i)            {                res[i*Col + j] = InputArray[index++];            }        }    }    else    {        for (int i = SquareWidth; i < Row; ++i)        {            for (int j = 0; j < Col; ++j)            {                res[i*Col + j] = InputArray[index++];            }        }    }    return res;}

显示二维数组元素:

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

测试函数:

int _tmain(int argc, _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;}

程序运行截图:
这里写图片描述
查找时,从二维数组的右上角开始,如果取出的元素v等于待查找的元素value,那么查找成功,返回;如果v

bool IsContained(const int Sorted2DArray[],const int Row,const int Col,const int value){    int i = 0,j = Col - 1;    while( i < Row && j >= 0)    {        int v = Sorted2DArray[i*Col + j];        if( v == value) return true;        else if( v < value)        {            if(i < Row) ++i;        }        else        {            if(j > 0) --j;        }    }    return false;}

测试函数:

int _tmain(int argc, _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);    cout<<IsContained(Sorted2DArray2,m,n,100)<<endl;    cout<<IsContained(Sorted2DArray1,n,m,46)<<endl;    return 0;}

程序运行截图:
这里写图片描述

0 0