折半查找
来源:互联网 发布:js 判断 arguments 编辑:程序博客网 时间:2024/05/16 07:37
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int binsearch(int a[], int n, int x)
{
int low, mid, high;
low = 0;
high = n - 1;
while (low <= high)
{
mid = (low + high) / 2;
if (x<a[mid])
{
high=mid - 1;
}
else if (x>a[mid])
{
low = mid + 1;
}
else
{
return mid;
}
}
return -1;
}
int main()
{
int x, result;
int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };//要查找的数据已经排好序
printf("请输入你要查找的数:");
scanf("%d", &x);
result = binsearch(arr, 10, x);
if (result < 0)
{
printf("没有找到\n");
}
else
{
printf("在arr[%d]找到%d\n", result, x);
}
system("pause");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int BinSearch(int a[], int x, int left, int right)
{
int middle;
if (left<=right)
{
middle = (left+right) / 2;
if (x < a[middle])
{
right = middle - 1;
}
else if (x>a[middle])
{
left = middle + 1;
}
else
{
return middle;
}
return BinSearch(a, x, left, right);
}
return -1;
}
int main()
{
int num, result;
int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };//要查找的数据已经排好序
printf("请输入你要查找的数:");
scanf("%d", &num);
result = BinSearch(arr, num, 0, 9);
if (result < 0)
{
printf("没有找到\n");
}
else
{
printf("在arr[%d]找到%d\n", result, num);
}
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int binsearch(int a[], int n, int x)
{
int low, mid, high;
low = 0;
high = n - 1;
while (low <= high)
{
mid = (low + high) / 2;
if (x<a[mid])
{
high=mid - 1;
}
else if (x>a[mid])
{
low = mid + 1;
}
else
{
return mid;
}
}
return -1;
}
int main()
{
int x, result;
int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };//要查找的数据已经排好序
printf("请输入你要查找的数:");
scanf("%d", &x);
result = binsearch(arr, 10, x);
if (result < 0)
{
printf("没有找到\n");
}
else
{
printf("在arr[%d]找到%d\n", result, x);
}
system("pause");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int BinSearch(int a[], int x, int left, int right)
{
int middle;
if (left<=right)
{
middle = (left+right) / 2;
if (x < a[middle])
{
right = middle - 1;
}
else if (x>a[middle])
{
left = middle + 1;
}
else
{
return middle;
}
return BinSearch(a, x, left, right);
}
return -1;
}
int main()
{
int num, result;
int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };//要查找的数据已经排好序
printf("请输入你要查找的数:");
scanf("%d", &num);
result = BinSearch(arr, num, 0, 9);
if (result < 0)
{
printf("没有找到\n");
}
else
{
printf("在arr[%d]找到%d\n", result, num);
}
system("pause");
return 0;
}
0 0
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 自定义垂直滚动切换TextView
- Context类浅析
- 简单银行管理系统
- 求数组n中出现次数超过一半的数
- 【学习笔记】day2数据存储和界面展现第一天 01_相对布局
- 折半查找
- 谈一谈自己对依赖、关联、聚合和组合之间区别的理解
- H-Index
- springmvc
- JAVA(1)——FLAG
- 283. Move Zeroes
- 数据结构-BST、AVL、二叉堆、B树、B+树、红黑树
- JQuery源码学习笔记(持续更新中)
- zookeeper原理