c# 二分查找法(2分钟算法)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication14
{
class Program
{
static void Main(string[] args)
{
int sequence = 0;
List<int> ints = new List<int>()
{
9,832,32,2,1,10,4
};
while (true)
{
Console.WriteLine("请输入要查找的值:");
bool found = false;
int a = int.Parse(Console.ReadLine());
int mid = 0;
int i = 0;
int j = ints.Count - 1;
while (i < j)
{
if (i == j && ints[i] != a)
{
break;
}
mid = (i + j) / 2;
if (ints[mid] == a)
{
sequence = mid;
found = true;
break;
}
if (ints[mid] > a)
{
i = mid + 1;
}
else
{
j = mid - 1;
}
}
if (found)
{
Console.WriteLine("找到:" + a + " 在序号" + (sequence+1));
}
else
{
Console.WriteLine("没找到。");
}
}
Console.ReadKey();
}
}
}
最新文章
- Python面向对象(一)
- Tomca不生产日志 (原创帖,转载请注明出处)
- JS实现继承的几种方式
- C#双链表
- Oracle 多实例如何通过EM进行访问-portlist.ini
- 网卡及MAC和PHY的区别
- Flume学习——BasicTransactionSemantics
- Sunday字符串匹配算法
- ExtJS初接触 —— 了解 Ext Core
- onmouseover事件
- HashMap与HashTable的区别、HashMap与HashSet的关系
- JavaScript基础知识(正则表达式、字符串)
- lvs与nginx区别
- JS 跳出多重循环
- 安装splash
- 搞明白GOROOT,GOPATH,GOBIN,project目录
- 坑人的 Javascript 模块化编程 require.js
- “Interrupted by header callback: Server reports Content-Length”如何解决
- 在商城系统中使用设计模式----策略模式之在spring中使用策略模式
- 跨域请求:JSONP
热门文章
- [BZOJ5133][CodePlus2017年12月]白金元首与独舞
- fpga产生伪随机序列
- serf &#160;简单使用
- rmmod: can&#39;t change directory to &#39;/lib/modules&#39;: No such file or directory
- MySQL中创建用户与授权
- TableView刷新 局部刷新等
- nginx学习资源
- mysql事务之一:MySQL数据库事务隔离级别(Transaction Isolation Level)及锁的实现原理
- python下载指定页面的所有图片
- 前端学习---css基本知识