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;
}

  

posted @ 2019-09-04 17:52  青衫客36  阅读(106)  评论(0编辑  收藏  举报