- 论坛徽章:
- 0
|
//折半查找
#include stdio.h>
#include stdlib.h>
#include string.h>
#include time.h>
//折半查找 非递归方法
//二分查找/折半查找 (基于有序表)
int binSearch(int *str, int target, int len)
{
int left = 1;
int right = len;
int mid;
while(left=right){
mid = (left+right)/2;
if(target == str[mid]) return mid;
else if(target > str[mid]) left = mid+1;
else right = mid - 1;
}
return -1;
}
//折半查找 递归方法
//二分查找/折半查找 (基于有序表)
int binSearch2(int *str, int target, int len)
{
// todo
return 0;
}
int main(void)
{
int len;
int a[20] = {0};
int i = 0;
printf("input string length: ");
scanf(" %d", &len);
//设置rand函数所用的启始种子值,以期每次产生的随机数序列均不相同。
srand(time(NULL));
while(i++ len){//a[0] 为哨岗
a = rand() % 100;
printf("%d ", a);
}
putchar('\n');
return 0;
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/5933/showart_278537.html |
|