算法随笔 - 求V型排序与倒V型排序数组的极值

现在来写一个求极值的算法,需求为:假设有一个整形数组,数值排列顺序为V型,即数值由大到小,再由小到大,例如int array[] = {100,99,93,92,91,89,12,9,7,5,3,1,6,8,87,123},现在要求的就是array的最小值,及其下标;类似的有倒V型排序的数组,求最大值及其下标,例如int array1[] = {1,2,3,4,12,34,45,56,78,5,3,2,1}。

    实现这个算法也比较简单,这里采用的是类二分查找法,下边直接看代码:

//头文件
#ifndef __LMSPUBLICLIB_H__
#define __LMSPUBLICLIB_H__

#include <stdio.h>
#include <unistd.h>
#include <string>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <vector>

using namespace std;

#ifndef MAX_PATH
#define MAX_PATH 256
#endif

namespace LmsPublicLib
{
    void FindExtremum(const int array[], int len, int &value, int &index, bool findMinValue = true);
};

#endif //__LMSPUBLICLIB_H__
//源文件
#include "LmsPublicLib.h"

namespace LmsPublicLib
{
    /*
    * array:查找的数组
    * len:数组长度
    * value:查找到的极值
    * index:查找到的极值的坐标
    * findMinValue: true 为查找V型排序的最小值,false为查找倒V型排序的最大值
    */
    void FindExtremum(const int array[], int len, int &value, int &index, bool findMinValue)
    {
        if(len <2)
        {
            return;
        }
        
        int minIndex = 0;
        int maxIndex = len-1;
        int midIndex = (maxIndex - minIndex)/2;

        while( (maxIndex - minIndex)>1 )
        {
            if( array[midIndex] > array[midIndex+1] )
            {
                if(findMinValue)    //查找的是最小值
                {
                    minIndex = midIndex;
                }
                else                //查找的是最大值
                {
                    maxIndex = midIndex;
                }
            }
            else
            {
                if(findMinValue)    //查找的是最小值
                {
                    maxIndex = midIndex;
                }
                else                //查找的是最大值
                {
                    minIndex = midIndex;
                }                
            }
            midIndex = (maxIndex - minIndex)/2 + minIndex;
        }

        if(findMinValue)
        {
            index = array[maxIndex]<array[minIndex] ? maxIndex : minIndex;
        }
        else
        {
            index = array[maxIndex]>array[minIndex] ? maxIndex : minIndex;
        }
        
        value = array[index];    
    }
}
//main.cpp测试程序
#include "LmsPublicLib.h"
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{   
    int array[] = {100,99,93,92,91,89,12,9,7,5,3,1,6,8,87,123};
    int array1[] = {1,2,3,4,12,34,45,56,78,5,3,2,1};

    int value = -1;
    int index = -1;
    LmsPublicLib::FindExtremum(array, sizeof(array)/sizeof(int), value, index);

    cout << "array minValue = " << value << endl;
    cout << "index = " << index << endl << endl;

    LmsPublicLib::FindExtremum(array1, sizeof(array1)/sizeof(int), value, index, false);

    cout << "array1 maxValue = " << value << endl;
    cout << "index = " << index << endl;

    return 0;
}

编译执行如下:

《算法随笔 - 求V型排序与倒V型排序数组的极值》

从结果看出,符合我们的预期,当然也可以考虑其他的方式,欢迎留言讨论。

 

 

    原文作者:Z字形编排问题
    原文地址: https://blog.csdn.net/lms1008611/article/details/87638618
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞