算法之二分查找(c语言版实现+测试)
来源:互联网 发布:评论滚动效果php 编辑:程序博客网 时间:2024/05/31 13:14
递归算法(deep是为了测试,实际生产不需要):
核心算法:
//递归算法int rank(Type key,Type a[],int lo,int hi,int deep){ if(lo > hi) return -1; int mid = lo + (hi - lo) / 2; printf("deep: %d lo: %d hi: %d\n",deep,lo,hi); if (key > a[mid]) { ++deep; return rank(key,a,mid + 1,hi,deep); } else if (key < a[mid]) { ++deep; return rank(key,a,lo,mid - 1,deep); } else return mid;}int BinarySearch(Type key,Type a[],int length){ int deep = 0; return rank(key,a,0,length,deep);}
辅助排序函数(快速排序):
void QuickSort(Type a[],int left,int right){ Type temp; if(left < right) { int i = left; int j = right; temp = a[left]; do { while(i < j && a[j] > temp) --j; if(i < j) a[i++] = a[j]; while(i < j && a[i] < temp) ++i; if(i < j) a[j--] = a[i]; }while(i != j); a[i] = temp; QuickSort(a,left,i-1); QuickSort(a,i + 1,right); }}
测试代码:
void test(char *mode,char *file){ //得到test 数组 FILE *fp; if ((fp = fopen(file,"r")) == NULL) { return; } int count = 0; int temp; while (fscanf(fp,"%d",&temp) != EOF) ++count; fseek(fp,0,SEEK_SET); Type a[count]; int pos = 0; while(fscanf(fp,"%d",&a[pos++]) != EOF); QuickSort(a,0,count - 1); Type key; if(strcmp(mode,"exist") == 0) { while(scanf("%d",&key) != EOF) { if(BinarySearch(key,a,sizeof(a)/sizeof(Type)-1) != -1) { printf("the key %d is exist in %s\n",key,file); } } } if(strcmp(mode,"noexist") == 0) { while(scanf("%d",&key) != EOF) { if(BinarySearch(key,a,sizeof(a)/sizeof(Type)-1) != -1) { printf("the key %d is not exist in %s\n",key,file); } } }}
main.c
int main(){ test("exist","test.txt"); return 0;}
非递归算法:
//非递归算法int BinarySearch(Type key,Type a[],int hi){ int lo = 0; while(lo <= hi) { int mid = lo + (hi - lo) / 2; if (a[mid] > key) hi = mid - 1; else if (a[mid] < key) lo = mid + 1; else return mid; } return -1;}
辅助排序函数(快速排序):
void QuickSort(Type a[],int left,int right){ Type temp; if(left < right) { int i = left; int j = right; temp = a[left]; do { while(i < j && a[j] > temp) --j; if(i < j) a[i++] = a[j]; while(i < j && a[i] < temp) ++i; if(i < j) a[j--] = a[i]; }while(i != j); a[i] = temp; QuickSort(a,left,i-1); QuickSort(a,i + 1,right); }}
测试代码:
void test(char *mode,char *file){ //得到test 数组 FILE *fp; if ((fp = fopen(file,"r")) == NULL) { return; } int count = 0; int temp; while (fscanf(fp,"%d",&temp) != EOF) ++count; fseek(fp,0,SEEK_SET); Type a[count]; int pos = 0; while(fscanf(fp,"%d",&a[pos++]) != EOF); QuickSort(a,0,count - 1); Type key; if(strcmp(mode,"exist") == 0) { while(scanf("%d",&key) != EOF) { if(BinarySearch(key,a,sizeof(a)/sizeof(Type)-1) != -1) { printf("the key %d is exist in %s\n",key,file); } } } if(strcmp(mode,"noexist") == 0) { while(scanf("%d",&key) != EOF) { if(BinarySearch(key,a,sizeof(a)/sizeof(Type)-1) != -1) { printf("the key %d is not exist in %s\n",key,file); } } }}
main.c
int main(){ test("exist","test.txt"); return 0;}
阅读全文
0 0
- 算法之二分查找(c语言版实现+测试)
- C语言版二分查找算法
- 数据结构与算法(C语言版)__二分查找
- 算法之二分查找(java版实现加测试)
- 算法之二分查找(c++版实现+测试)
- 算法之二分查找(php版实现+测试)
- 线性查找,二分查找(C语言版)
- c/c++算法之正确实现二分查找
- 二分查找算法的C/C++实现
- C语言实现二分查找算法
- C语言实现二分查找算法
- 二分查找算法的C语言实现
- c语言实现二分查找算法
- 用C语言实现二分查找算法
- 算法之二分查找【C语言】
- 二分查找算法之python实现
- 算法学习之java实现二分查找
- Java实现算法之--二分查找法
- Mac
- Android post网络数据请求(本人新手版本)
- fix协议封装挑战-数据库脚本
- 【PSOC4】+内部资源充分利用--ADC、UART、LCD、CapSense等
- ARP:地址解析协议
- 算法之二分查找(c语言版实现+测试)
- scikit-learn/ID3算法使用GridSearchCV调优
- git命令探索之路
- Unity 三种遮挡关系
- Mac
- 打造Listview的万能适配CommonAdapter(方便以后查找)
- Android使用Intent
- chrome 调试小技巧
- Faster-rcnn voc数据集制作