MOOC 3.1 引子

#include <cstdio>

typedef struct LNode *List;
struct LNode
{
	ElementType Element[MAXSIZE];
	int length;
};

// 顺序查找 
int SequentialSearch(List Tb1, ElementType K)
{
	int i;
	Tb1->Element[0] = K;
	for(i = Tb1->length; Tb1->Element[i] != K; i --);
	return i;
}

// 二分查找
int BinarySearch(List Tb1, ElementType K)
{
	int left, right, NotFound = -1;
	left = 1;
	right = Tb1->length;
	while(left <= right)
	{
		mid = left + (right - left)/2;
		if(K < Tb1->Element[mid])	right = mid - 1;
		else if(K > Tb1->Element[mid])	left = mid + 1;
		else	return mid;
	}
	return NotFound;
}

  

猜你喜欢

转载自www.cnblogs.com/mjn1/p/11460635.html
3.1
今日推荐