采用二分查找法查找特定的元素。要求输入数组长度,输入数组元素和要查找的关键字。程序输出查找成功与否,平且输出查找成功时关键字在数组中的未指定。
技术要点:二分查找就是折半查找,基本思路是:取中间位置的记录,将其与要查找的key进行比较,若相等,则查找 成功。若key比其大,则要找的元素一定在右子表中,则继续对右子表进行折半查找。若key比其小,则要找的元素一定在左子表中,则继续对左子表进行折半查...
分类:
其他好文 时间:
2014-12-07 12:39:41
阅读次数:
143
二分查找算法,又称折半查找,是一种在有序数组中查找某一特定元素的搜索算法。搜素过程从数组的中间元素开始,1)如果中间元素正好是要查找的元素,则搜素过程结束;2)如果某一特定元素大于或者小于中间元素,则在数组大于或小于中间元素的那一半中查找;3)数组为空,退出代码如下: 1 int binary...
分类:
其他好文 时间:
2014-12-07 12:31:03
阅读次数:
211
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
...
分类:
其他好文 时间:
2014-12-04 13:59:41
阅读次数:
154
二叉排序树的查找算法
假定二叉排序树的根节点指针为root,给定的关键字值为K,则查找算法可描述为:
置初值:p = root ;
如果 key = p -> data ,则查找成功,算法结束;
否则,如果key data ,而且 p 的左子树非空,则将 p 的左子树根送 p ,转步骤 2 ;否则,查找失败,算法结束;
否则,如果 key > p->data ,而且...
分类:
编程语言 时间:
2014-12-04 12:16:31
阅读次数:
107
#include
#include
#include
int a[]={223, 34, 23, 2, 21, 55, 87, 533 , 213, 111};
//int a[]={2, 21, 23, 34, 55, 87, 111, 213, 223, 533};
//int a[]={533, 223, 213, 111, 87, 55, 23, 34 , 2...
分类:
编程语言 时间:
2014-12-03 15:48:07
阅读次数:
145
/* 斐波那契查找法 */
#include
#include
int Fib( int k )
{
if( 1 == k || 2 == k )
return 1;
else
return Fib(k-1)+Fib(k-2);
}
int FibSearch( int *a, int n, int key )
{
int k = 1;
int nFib;
int *b...
分类:
编程语言 时间:
2014-12-01 22:33:54
阅读次数:
259
折半查找是一种比较高效的查找方式,其基本思想是:在某个有序表中,取出中间的记录作为比较对象,如果要查找记录的关键码等于中间记录的关键码,则查找成功;若要查找记录的关键码小于中间记录的关键码,则在中间记录的左半区继续查找;若查找记录的关键码大于中间记录的关键码,则在中间记录的右半区继续查找。不断重复....
分类:
编程语言 时间:
2014-12-01 22:22:41
阅读次数:
158
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted from left to right.
The first integer of each row...
分类:
其他好文 时间:
2014-11-30 16:56:56
阅读次数:
120
#include
#include
#include
using namespace std;
void array_sort(int a[], int n);
int zhebancz(int a[], int n,int num);
int main()
{
int a[15];
int n,i;
srand( (unsigned)time( NULL ) );
for(i=0...
分类:
其他好文 时间:
2014-11-29 11:55:23
阅读次数:
199
#includeusing namespace std;int main(){ int a[10]={1,2,3,4,5,21,34,45,115,4121}; int f,tail=0,top=9,mid=(top+tail)/2; int n; cout>n; wh...
分类:
其他好文 时间:
2014-11-27 00:00:52
阅读次数:
367