C语言 mid 线性表,求线性表查找算法的代码,从1000个数查找一个数

[C++] 纯文本查看 复制代码#include

#include

#include

#include

#define MAX_ELEMENT_SIZE 1000

#define N 500

int g_nCount = 0;

typedef struct array

{

int *arr;

int len;

}array;

namespace wind_test

{

// 排序

int sort(struct array *pArray)

{

if (pArray == nullptr || pArray->arr == nullptr)

return -1;

int temp;

for (int i = 0; i < pArray->len - 1; i++)

{

for (int j = i + 1; j < pArray->len; j++)

{

if (pArray->arr[i] > pArray->arr[j])

{

temp = pArray->arr[i];

pArray->arr[i] = pArray->arr[j];

pArray->arr[j] = temp;

}

}

}

return 0;

}

// 顺序查找

int order_search(struct array *pArray, int key)

{

if (pArray == nullptr || pArray->arr == nullptr)

return -1;

g_nCount = 0;

// 暴力搜索即可

for (int i = 0; i < pArray->len; i++)

{

++g_nCount;

if (pArray->arr[i] == key)

return i;

}

return -1;

}

// 折半查找

int bin_search(struct array *pArray, int key)

{

if (pArray == nullptr || pArray->arr == nullptr)

return -1;

g_nCount = 0;

int low = 0;

int mid = 0;

int high = pArray->len-1;

while(low <= high)

{

++g_nCount;

// 分成两半

mid = (low+high) / 2;

// 如果刚好是就返回

if(key == pArray->arr[mid])

return mid;

// 如果大于就找右边

else if(key > pArray->arr[mid])

low = mid+1;

// 如果小于就找左边

else

high = mid-1;

}

return -1;

}

// 初始化线性表

struct array *create_arr()

{

// new出线性表

struct array *pArray = new array;

if (pArray == nullptr)

{

printf("aa\n");

return nullptr;

}

memset(pArray, 0, sizeof(struct array));

// new出1000个元素

pArray->arr = new int[MAX_ELEMENT_SIZE];

if (pArray->arr == nullptr)

{

printf("bb\n");

delete pArray;

pArray = nullptr;

return nullptr;

}

memset(pArray->arr, 0, MAX_ELEMENT_SIZE);

// 放入元素

for (int i = 0; i < MAX_ELEMENT_SIZE; i++)

pArray->arr[i] = rand() % MAX_ELEMENT_SIZE;

pArray->len = MAX_ELEMENT_SIZE;

return pArray;

}

void destroyResource(struct array *pArray)

{

if (pArray == nullptr || pArray->arr == nullptr)

return;

delete []pArray->arr;

pArray->arr = nullptr;

delete pArray;

}

}

int main(void)

{

srand((unsigned int)time(nullptr));

struct array *pArray;

pArray = wind_test::create_arr();

printf("创建线性表,排序之前:\n");

for (int i = 0; i < MAX_ELEMENT_SIZE; i++)

printf("%d ", pArray->arr[i]);

printf("\n");

wind_test::sort(pArray);

printf("排序之后:\n");

for (int i = 0; i < MAX_ELEMENT_SIZE; i++)

printf("%d ", pArray->arr[i]);

printf("\n");

printf("顺序查找结果如下:\n");

int index;

if ((index = wind_test::order_search(pArray, N)) != -1)

printf("存在该元素,下标 = %d\n", index);

else

printf("不存在该元素\n");

printf("顺序查找遍历次数 = %d\n", g_nCount);

printf("折半查找结果如下:\n");

if ((index = wind_test::bin_search(pArray, N)) != -1)

printf("存在该元素,下标 = %d\n", index);

else

printf("不存在该元素\n");

printf("折半查找遍历次数 = %d\n", g_nCount);

wind_test::destroyResource(pArray);

pArray = nullptr;

return 0;

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值