星海's Blog

老头初学编程
合理的有理数实现 代码
求2的N次方(要求,最简算法复杂度为lgn)

折半算法

星海 posted @ 2010年12月05日 13:32 in C代码 , 1442 阅读

 

/*
 *       Filename:  binary.c
 *    Description: 从一组排序好的序列里找出某个元素的位置(折半查找) 
 * ==========================================================================
 */
#include <stdio.h>
#define LEN 8

int a[LEN] = { 1, 2, 2, 5, 5, 6, 8, 9 };

int binary(int number)
{
	int mid, start = 0, end = LEN - 1;

	while (start <= end) {
		mid = (start + end) / 2;
		if (a[mid] < number)
			start = mid + 1;
		else if (a[mid] > number)
			end = mid - 1;
		else {
			while (a[mid] == number) {
				--mid;
			};	//返回最早出现的匹配位值
			return mid + 1;
		}
	}
	return -1;
}

int main(void)
{
	printf("%d\n", binary(2));
	return 0;
}

 

Avatar_small
civaget 说:
2023年12月11日 23:46

I had the best 러시아마사지 session at home. Convenience and quality, what more could you ask for?

Avatar_small
civaget 说:
2023年12月12日 03:13

구글 seo, with its intricate web of strategies, offers websites the chance to not just exist online but to thrive by dominating search engine rankings.

Avatar_small
civaget 说:
2023年12月12日 19:40

제주오피 combines work and leisure seamlessly. It's the ultimate accommodation for a Jeju adventure.

Avatar_small
civaget 说:
2023年12月14日 19:05

OP Guide's user-friendly platform makes finding the ideal 출장샵 effortless, ensuring you get the pampering you need.

Avatar_small
civaget 说:
2023年12月14日 23:59

This can be a very amazing powerful resource that you’re offering and you just provide it away cost-free!! I really like discovering websites ones view the particular importance of supplying you with a superb learning resource for zero cost. We truly dearly loved examining this page. Appreciate it! 실시간스포츠중계

Avatar_small
civaget 说:
2023年12月18日 02:57

티비위키 has become my primary source for OTT entertainment.

Avatar_small
civaget 说:
2023年12月19日 01:35

I've shared 누누티비 with my colleagues, and they're now devoted users.

Avatar_small
civaget 说:
2023年12月20日 03:09

The peaceful ambiance at 구미휴게텔 is perfect for unwinding.

Avatar_small
civaget 说:
2023年12月25日 21:57

Explore 해외스포츠중계 for an immersive sports journey like no other.

Avatar_small
civaget 说:
2023年12月26日 22:03

very nice post, i definitely enjoy this fabulous website, persist with it 프리카지노

Avatar_small
civaget 说:
2023年12月27日 04:36

Self-published authors can use online platforms to offer exclusive content to dedicated readers. self publishing on amazon

Avatar_small
civaget 说:
2023年12月31日 13:50

Interesting website, i read it but i still have a few questions. shoot me an email and we will talk more becasue i may have an interesting idea for you. Divine Revelations

Avatar_small
civaget 说:
2024年1月08日 22:51

출장마사지 has become an essential part of my self-care routine.

Avatar_small
civaget 说:
2024年1月15日 22:25

The way you write, you are really a professional blogger.:”~`: ufaคืนยอดเสีย


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter