C算法——查找 顺序查找算法

源码+注释

//
// Created by Lenovo on 2022-05-27-上午 10:05.
// 作者:小象
// 版本:1.0
//

#include <malloc.h>
#include "stdio.h"
#include <stdlib.h>


#define MAXSIZE 2147483647 // int型0~21亿 顺序表可能达到的最大长度10位
#define OK 1
#define ERROR 0

// 数据元素类型定义
typedef struct {
    int key; // 关键字域
    int otherInfo; // 其他域
} ElemType;

// 顺序表定义
typedef struct {
    ElemType *elem; // 存储空间的基地址
    int length; // 当前长度
} SqSTable;

int InitTable(SqSTable *table); // 顺序表的初始化
int CreateTable(SqSTable *table, int length); // 顺序表的创建
void knuthShuffle(SqSTable *table); // 洗牌算法
void swapInt(int *card_1, int *card_2); // 交换函数
int SearchSeq_01(SqSTable table, int key); // 顺序查找方法一
int SearchSeq_02(SqSTable table, int key); // 顺序查找方法一

/**
 * <h2>顺序查找</h2>
 * @return 0
 */
int main() {

    SqSTable table;
    if (!(InitTable(&table))) {
        printf("空间申请失败!!!");
    }
    // 1-122222
    CreateTable(&table, 122222);

    printf("顺序查找一结果:%s \n", SearchSeq_01(table, 122223) == 0 ? "没有找到" : "找到了");

    printf("顺序查找二结果:%s \n", SearchSeq_02(table, 0) == 0 ? "没有找到" : "找到了");

    getchar();
}

// 构造一个空的顺序表
int InitTable(SqSTable *table) {
    table->elem = (ElemType *) malloc(sizeof(SqSTable) * MAXSIZE); // 动态方式需要先分配内存,而且
    // 需要用到malloc函数申请,有可能申请不到,申请到后malloc()函数会把申请到的连续数据空间首地址返回
    // 注意:这里申请到返回的地址是十六进制的地址,而elem只能存十进制的地址,所以需要强转为十进制地址后赋值
    if (table->elem == NULL) { // 如果没有申请到地址,退出
        return ERROR;
    }
    table->length = 0; // 空表长度为 0
    return OK; // 初始化成功
}

// 顺序表的创建
int CreateTable(SqSTable *table, int length) {
    if (table->length != 0) { // 顺序表不为空,不创建
        return ERROR;
    }
    for (int i = 1; i <= length; i++) {
        table->elem[i].key = i;
        table->length++; // 表长加 1
    }
    knuthShuffle(table);
//    for (int i = 1; i <= length; i++) {
//        printf("%d\t", table->elem[i].key);
//        if (i % 10 == 0) {
//            printf("\n");
//        }
//    }
    printf("\n");
    return OK;
}

void knuthShuffle(SqSTable *table) {
    for (int i = table->length; i >= 1; i--) {
        swapInt(&(table->elem[i].key), &(table->elem[rand() % (i + 1) + 1].key));
    }
}

void swapInt(int *card_1, int *card_2) {
    int tCard;
    tCard = *card_1;
    *card_1 = *card_2;
    *card_2 = tCard;
}

// 在顺序表table中顺序查找其关键字等于key的数据元素。若找到,则函数值为该元素在表中的位置,否则为0
int SearchSeq_01(SqSTable table, int key) {
    for (int i = table.length; i >= 1; i--) { // 直接的条件限制:不能查找到第一个数(第一个数默认不使用)
        if (table.elem[i].key == key) {
            return i; // 从后往前找
        }
    }
    return 0;
}

// 在顺序表sqSTable中顺序查找其关键字等于key的数据元素。若找到,则函数值为该元素在表中的位置,否则为0
int SearchSeq_02(SqSTable table, int key) {
    int i;
    table.elem[0].key = key; // 第一个数的作用来了 -> 设置 “哨兵”
    for (i = table.length; table.elem[i].key != key; i--); // 从后往前找
    return i;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小丶象

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值