⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 search.h

📁 经典数据结构书籍 数据结构C++语言描述 的源代码 很难找的哦
💻 H
字号:
#ifndef TEMPLATE_UTILITY_FUNCTIONS
#define TEMPLATE_UTILITY_FUNCTIONS

#include <string.h>

// using key, search the n element arrray list for a match.
// if found, return a pointer to the data; otherwise return NULL 
template <class T>
int SeqSearch(T list[], int n, T key)
{
    for(int i=0;i < n;i++)
       if (list[i] == key)
            return i;    // return index to matching item
    return -1;            // search failed. return -1
}

#include "array.h"

// recursive version of the binary search to locate
// a key in an ordered array A
template <class T>
int BinSearch(T A[], int low, int high, T key)
{
    int mid;
    T midvalue;

    // key not found is a stopping condition
    if (low > high)
        return(-1);

    // compare against list midpoint and subdivide
    // if a match does not occur. apply binary
    // search to the appropriate sublist
    else
    {
        mid = (low+high)/2;
        midvalue = A[mid];
        // stopping condition if key matched
        if (key == midvalue)
            return(mid);    // key found at index mid

        // look left if key < midvalue; otherwise, look right
        else if (key < midvalue)
            // recursive step
            return BinSearch(A,low,mid-1,key);
        else
            // recursive step
            return BinSearch(A,mid+1,high,key);
    }
}

#endif  // TEMPLATE_UTILITY_FUNCTIONS

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -