C++:查找

一、顺序查找

#include <iostream>
using namespace std;

template<class T>
int SeqSearch(T A[], int n, T key)
{
    for (int i = 0; i < n; i++)
    {
        if (A[i] == key)
        {
            return i; // 如果查找成功返回节点下标
        }
    }
    return -1; // 如果失败返回-1
}
 
int main()
{
    int A[100];
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> A[i];
    }

    int goal;
    cin >> goal;
    cout << endl;

    cout << SeqSearch(A, n, goal) << endl;
    
    return 0;
}
 
/*
测试
17
59 63 60 72 80 51 56 28 30 22 45 -13 20 7 12 9 16
28

17
59 63 60 72 80 51 56 28 30 22 45 -13 20 7 12 9 16
8
*/

二、二分查找

#include <iostream>
#include<algorithm>
using namespace std;

// 二分查找需要目标序列已经排好序
template<class T>
int BinSearch(T A[], int n, T key)
{
    int low, high, mid;
    // 设置初始查找区域
    low = 0;
    high = n - 1;
    
    while(low <= high)
    {
        mid = (low + high) >> 1;
        if (key == A[mid])
        {
            return mid; //找到则返回节点下标
        }
        else if (key > A[mid]) // 中间值比目标值小
        {
            low = mid + 1; // 向上缩小查找区域
        }
        else // 中间值比目标值大
        {
            high = mid - 1; // 向下缩小查找区域
        }
    }
    return -1; // 若未找到则返回-1
}
 
int main()
{
    int A[100];
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> A[i];
    }
    // 先对目标序列排序
    sort(A, A + n + 1);

    int goal;
    cin >> goal;
    cout << endl;

    cout << BinSearch(A, n, goal) << endl;
    
    return 0;
}
 
/*
测试
17
59 63 60 72 80 51 56 28 30 22 45 -13 20 7 12 9 16
28

17
59 63 60 72 80 51 56 28 30 22 45 -13 20 7 12 9 16
8
*/

三、分块查找

#include<bits/stdc++.h>
using namespace std;

template<class T>
class node
{
public:
    T data;
    node * next;
};

// 创建块结构
template<class T>
node<T> * BlockCreate(T A[], int n, int amount) // amount为分块数量
{
    // 建立索引表
    node<T> * blocks = new node<T>[amount];
    // 确立索引值
    int len = n / amount + 1;
    int k = len;
    int i = 0;
    while(k <= n)
    {
        blocks[i].data = A[k];
        blocks[i].next = NULL;
        i++;
        k += len;
    }

    // 块间排序
    for (int j = 1; j < amount; j++)
    {
        T temp = blocks[j].data;
        int l;
        for (l = j; l >= 1; l--)
        {
            if (blocks[l - 1].data > temp)
            {
                blocks[l].data = blocks[l - 1].data;
            }
            else
            {
                break;
            }
        }
        blocks[l].data = temp;
    }

//    //输出索引表
//    for (int j = 0; j < amount; j++)
//    {
//        cout << blocks[j].data << endl;
//    }
//    cout << endl;
    
    // 数据分块
    for (i = 0; i < n; i++)
    {
        // 确定应该存储到的块
        k = 0;
        while(k < amount)
        {
            if (A[i] < blocks[k].data)
            {
                node<T> * tmp = new node<T>;
                tmp->data = A[i];
                tmp->next = NULL;
                for (node<T> * p = &blocks[k]; p != NULL; p = p->next)
                {
                    if (p->next == NULL)
                    {
                        p->next = tmp;
                        break;
                    }
                }
                break;
            }
            else
            {
                k++;
            }
        }
    }

    
    return blocks;
}

//在创建的块中进行查找
template<class T>
bool BlockSearch(T goal, node<T> * blocks, int amount)
{
    // 确定可能所在区块
    int k = 0;
    while(k < amount)
    {
        if (goal <= blocks[k].data)
        {
            for (node<T> * p = &blocks[k]; p != NULL; p = p->next)
            {
                if (goal == p->data)
                {
                    return 1;
                }
            }
        }
        k++;
    }
    return 0;
}
 
int main()
{
    int A[100];
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> A[i];
    }
    int goal;
    cin >> goal;
    cout << endl;
    int amount = sqrt(n) + 1;
    node<int> * head = BlockCreate(A, n, amount);
    
    cout << BlockSearch(goal, head, amount);
    
    return 0;
}
 
/*
测试
17
59 63 60 72 80 51 56 28 30 22 45 -13 20 7 12 9 16
28

17
59 63 60 72 80 51 56 28 30 22 45 -13 20 7 12 9 16
8
*/

四、散列查找

#include<bits/stdc++.h>
using namespace std;

template<class T>
class node
{
public:
    T data;
    node * next;
    
    node()
    {
        data = -1;
        next = NULL;
    }    
};

// 确定取模数
int GetP(int len)
{
    // 选取模数p为不大于len的最大素数
    int p = len;
    while(1)
    {
        bool flag = 1;
        for (int i = p - 1; i > 1; i--)
        {
            if (p % i == 0)
            {
                flag = 0;
                break;
            }
        }
        if (flag == 1)
        {
            break;
        }
        else
        {
            p--;
        }
    }
    return p;
}

// 假定需要查找的数据都为正整数
// 创建散列表
template<class T>
node<T> * HashCreate(T A[], int n, int len, int p) // len为散列表长度
{    
    node<T> * table = new node<T>[len];
    for (int i = 0; i < n; i++)
    {
        int index = A[i] % p;
        // 若不出现冲突,直接放入散列表中
        if (table[index].data == -1)
        {
            table[index].data = A[i];
        }
        // 若出现冲突,这里选择使用独立链表地址法解决冲突
        else
        {
            node<T> * tmp = new node<T>;
            tmp->data = A[i];
            tmp->next = NULL;
            for (node <T>* p = &table[index]; p != NULL; p = p->next)
            {
                if (p->next == NULL)
                {
                    p->next = tmp;
                    break;
                }
            }
        }
    }
    return table;
}

// 进行散列查找
template<class T>
bool HashSearch(node<T> * table, int p, T goal)
{
    // 计算关键字
    int key = goal % p;
    
    // 在散列表中进行查找
    bool flag = 0;
    if (table[key].data == -1)
    {
        return 0;
    }
    else
    {
        node<T> * tmp = &table[key];
        while(1)
        {
            if (tmp->data == goal)
            {
                return 1;
            }
            else
            {
                if (tmp->next == NULL)
                {
                    return 0;
                }
                else
                {
                    tmp = tmp->next;
                }
            }
        }
    }

}

int main()
{
    int A[100];
    int n;
    cin >> n;
    
    for (int i = 0; i < n; i++)
    {
        cin >> A[i];
    }
    
    int goal;
    cin >> goal;
    cout << endl;
    
    int len;
    cout << "输入散列表长度:";
    cin >> len;
    
    int p = GetP(len);
    node<int> * table = HashCreate(A, n, len, p);
    
    cout << HashSearch(table, p, goal);
    
    return 0;
}
 
/*
测试
17
59 63 60 72 80 51 56 28 30 22 45 13 20 7 12 9 16
28

17
59 63 60 72 80 51 56 28 30 22 45 13 20 7 12 9 16
26
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值