二叉排序和二叉查找
来源:互联网 发布:c语言第五版 编辑:程序博客网 时间:2024/05/18 03:10
#include <iostream>
using namespace std;
int Bsearch(int a[],int low,int high,int t)
{
while(low<=high)
{
int mid=(low+high)/2;
if(a[mid]==t)
{
return mid;
}
if(a[mid]<t)
{
low=mid+1;
}
else
{
high=mid-1;
}
}
return -1;
}
void Bsort(int a[],int n)
{
for(int i=0; i<n; i++)
{
int temp=a[i];
int low=0;
int high=i-1;
while(low<=high)
{
int mid=(low+high)/2;
if(a[mid]<temp)
{
low=mid+1;
}
else
{
high=mid-1;
}
}
for(int j=i-1;j>high;j--)
{
a[j+1]=a[j];
}
a[high+1]=temp;
}
}
int main()
{
int a[5]= {1,2,3,4,0};
int t;
Bsort(a,5);
for(int i=0; i<5; i++)
cout<<a[i]<<endl;
while(cin>>t)
{
cout<<Bsearch(a,0,4,t)<<endl;
}
return 0;
}
using namespace std;
int Bsearch(int a[],int low,int high,int t)
{
while(low<=high)
{
int mid=(low+high)/2;
if(a[mid]==t)
{
return mid;
}
if(a[mid]<t)
{
low=mid+1;
}
else
{
high=mid-1;
}
}
return -1;
}
void Bsort(int a[],int n)
{
for(int i=0; i<n; i++)
{
int temp=a[i];
int low=0;
int high=i-1;
while(low<=high)
{
int mid=(low+high)/2;
if(a[mid]<temp)
{
low=mid+1;
}
else
{
high=mid-1;
}
}
for(int j=i-1;j>high;j--)
{
a[j+1]=a[j];
}
a[high+1]=temp;
}
}
int main()
{
int a[5]= {1,2,3,4,0};
int t;
Bsort(a,5);
for(int i=0; i<5; i++)
cout<<a[i]<<endl;
while(cin>>t)
{
cout<<Bsearch(a,0,4,t)<<endl;
}
return 0;
}
0 0
- 二叉排序和二叉查找
- Java 实现二叉树排序和查找
- 二叉排序(查找)树
- 二叉排序(查找)树
- 二叉排序/查找树
- 二叉排序查找树
- 排序二叉树查找
- 二叉树排序查找
- 用二叉查找数排序
- 二叉查找树实现排序
- 二叉查找(排序)树
- 二叉查找(排序)树
- 算法-二叉树查找排序
- 排序和查找系统(快排,二叉搜索)
- 二叉树和二叉查找树
- 数据结构-二叉树和二叉查找树
- 二叉树和二叉查找树
- 二叉树和二叉查找树
- yum install error – GPG key retrieval failed
- grails 记录
- 大数据工程师需要的东西
- angular.js 错误:Error: [$injector:modulerr] 原因
- C#使用UdpClient发送和接收UDP数据示例
- 二叉排序和二叉查找
- log4j.properties配置详解
- git [packet_write_wait connection to xx.xx.xx.xx Broken pipe]解决办法
- cc2541之pwm笔记
- redux的connect解读2
- 解决:The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or t
- EM推导
- mysql5.7 修改密码update password提示找不到password字段
- C# 中Struct与Class区别