查找

作者: RivenL | 来源:发表于2017-04-30 23:02 被阅读0次

顺序查找

int sequenceSearch(int a[], int count, int targetValue) {
    if (!a || count <=0) return -1;

    for (int i=0; i<count; i++) {
        if (targetValue == a[i]) return i;
    } 
    
    return -1;
}

二分查找

int binarySearch(int a[], int count, int targetValue) {
    
    if (!a || count<=0) return -1;

    return iterativeBinarySearch(a, count, targetValue);
    //return recursiveBinarySearch(a, targetValue, 0, count - 1);
}

int recursiveBinarySearch(int a[], int targetValue, int low, int high) {
    int middleIndex = 0    

    if (!a || low > high) return -1;
    
    middleIndex = low + (hight-low) / 2;
    if (a[middleIndex] == targetValue) return middleIndex;
    else if (a[middleIndex] < targetValue) 
        return recursiveBinarySearch(a, targetValue, middleIndex+1, hight);
    else return recursiveBinarySearch(a, targetValue, low, middleIndex-1);
}

int iterativeBinarySearch(int a[], int count, int targetValue) {
    int middleIndex = 0;
    int row = 0, high = count - 1;
    
    if (!a || count <= 0) return -1;

    while (row <= high) {
        middleIndex = low + (high - low) / 2;
        if (a[middleIndex] == targetValue) return middleIndex;
        else if (a[middleIndex] < targetValue) low = middleIndex + 1;
        else high = middleIndex - 1;
    }

    return -1;
}

插值查找

int insertSearch(int a[], int count, int targetValue) {
  return recursiveInsertSearch(a,  targetValue, 0, count - 1);
}

int recursiveInsertSearch(int a[], int targetValue, int low, int high) {
    int middleIndex = 0;
    
    if (!a || low > high) return -1;

    middleIndex = low + (targetValue-a[low]) / ((a[high]-a[low])/(high-low));
    if (a[middleIndex] == targetValue) return middleIndex;
    else if(a[middleIndex] < targetValue) 
        return recursiveInsertSearch(a, targetValue, middleIndex+1, high);
    else return recursiveInsertSearch(a, targetValue, low, middleIndex-1);
}

查找子数组最大和

/*
    输入一个整形数组,数组里有正数也有负数。
    数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。
    求所有子数组的和的最大值。要求时间复杂度为O(n)。
    例如输入的数组为1, -2, 3, 10, -4, 7, 2, -5,和最大的子数组为3, 10, -4, 7, 2,
    因此输出为该子数组的和18。
*/
 int maxSumSubArray(int a[], int count) {
    int max = -1, sum = 0;
    
    if (!a || count <= 0) return -1;

    for (int i=0; i<count; i++) {
        sum += a[i];
        if (sum < 0) {
            sum = 0;
            continue;
        }
        if (max > sum) max = sum;
    }

    return max;
}

相关文章

  • 《数据结构与算法》知识点(四)

    第七章 查找 顺序查找、折半查找、索引查找、分块查找是静态查找,动态查找有二叉排序树查找,最优二叉树查找,键树查找...

  • 查找

    静态查找顺序查找 折半查找 散列查找 动态查找二叉排序树 散列查找 ASL(平均查找长度) - 衡量查找算法效率的...

  • PHP查找算法

    静态查找 顺序查找 折半查找 递归折半查找

  • 6.1 查找算法_基础

    1. 查找基本概念 查找:只有两种情况,查找成功,查找失败 查找表:查找的数据集合称为查找表 静态查找表 / 动态...

  • 据结构与算法学习-查找与二叉排序树

    查找表操作方式分为静态查找和动态查找。静态查找表(Static Search Table): 只作查找操作的查找表...

  • iOS-字符串查找

    字符串查找通常有四种方式,暴力查找,KMP查找,BoyerMoore查找以及RabinKarp算法查找,查找最简单...

  • linux 查找目录或文件

    查找目录:find /(查找范围) -name '查找关键字' -type d查找文件:find /(查找范围) ...

  • Linux查找文件、文件夹

    查找目录:find /(查找范围) -name '查找关键字' -type d查找文件:find /(查找范围) ...

  • linux常用命令

    查找目录:find /(查找范围) -name '查找关键字' -type d查找文件:find /(查找范围) ...

  • linux查找文件夹、文件

    查找目录:find /(查找范围) -name '查找关键字' -type d 查找文件:find /(查找范围)...

网友评论

      本文标题:查找

      本文链接:https://www.haomeiwen.com/subject/pfwxtxtx.html