栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构:查找

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构:查找

 

 

 

#include 
#include 

// 迭代实现。
// 在sstable中查找key,失败返回-1,成功返回key在sstable中的数组下标。
int Bin_Search1(int *sstable,unsigned int len,int key)
{
  int low,high,mid;
  low=0; high=len-1;    // 初始化low和high的值,设置初始区间。

  while (low<=high)
  {
    mid=(low+high)/2;   // 计算mid的值,取中间位置。

    if (sstable[mid]==key) return mid;     // 找到了待查找的元素。
    else if (sstable[mid]>key) high=mid-1; // 继续在前半区间进行查找。
    else low=mid+1;                        // 继续在后半区间进行查找。
  }

  return -1;
}

// 递归实现。
// 在sstable中查找key,失败返回-1,成功返回key在sstable中的数组下标。
int Bin_Search2(int *sstable,int key,int low,int high)
{
  if (low>high) return -1;  

  int mid=(low+high)/2;   // 计算mid的值,取中间位置。

  if (sstable[mid]==key) return mid;         // 找到了待查找的元素。
  else if (sstable[mid]>key) Bin_Search2(sstable,key,low,mid-1); // 继续在前半区间进行查找。
  else Bin_Search2(sstable,key,mid+1,high);  // 继续在后半区间进行查找。
}

int main()
{
  // 有序的顺序表。
  int sstable1[]={1,2,3,4,5,6,7,8,9,12};
  int len=sizeof(sstable1)/sizeof(int);

  // 迭代
  printf("result1=%dn",Bin_Search1(sstable1,len,0));
  printf("result1=%dn",Bin_Search1(sstable1,len,1));
  printf("result1=%dn",Bin_Search1(sstable1,len,7));
  printf("result1=%dn",Bin_Search1(sstable1,len,10));
  printf("result1=%dn",Bin_Search1(sstable1,len,12));
  printf("result1=%dn",Bin_Search1(sstable1,len,15));
  printf("n");

  // 递归
  printf("result2=%dn",Bin_Search2(sstable1, 0,0,len-1));
  printf("result2=%dn",Bin_Search2(sstable1, 1,0,len-1));
  printf("result2=%dn",Bin_Search2(sstable1, 7,0,len-1));
  printf("result2=%dn",Bin_Search2(sstable1,10,0,len-1));
  printf("result2=%dn",Bin_Search2(sstable1,12,0,len-1));
  printf("result2=%dn",Bin_Search2(sstable1,15,0,len-1));

  return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/722318.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号