博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 二分查询
阅读量:4709 次
发布时间:2019-06-10

本文共 1247 字,大约阅读时间需要 4 分钟。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 二分查询{    class Program    {        static void Main(string[] args)        {            int[] array = { 10, 20, 50, 6, 45, 10, 33, 25, 40, 5 };            Array.Sort(array);            Console.Write("数组排序之后: ");            foreach (var i in array)            {                Console.Write(i + ",");            }            Console.WriteLine();            int a = SearchFun(array, 45);            Console.WriteLine("找到的值:" + a);            Console.Read();        }        static int SearchFun(int [] array,int value)        {            int mid, low, high;            low = 0;            high = array.Length - 1;            while (low < high)            {                mid = (low + high) / 2;                 //数组从中间找                if (array[mid] == value)                    return array[mid];                if (array[mid] > value)                 //数组中的值 大于 要找的值, 继续在数组下部分查询                       high = mid - 1;                else                    low = mid + 1;                      //数组中的值 大于 要找的值, 继续在数组上部分查询               }            return -1;        }    }}

转载于:https://www.cnblogs.com/plateFace/p/5196146.html

你可能感兴趣的文章
FindChildControl与FindComponent
查看>>
中国城市json
查看>>
android下载手动下载Android SDK
查看>>
C++学习:任意合法状态下汉诺塔的移动(原创)
查看>>
leetcode133 - Clone Graph - medium
查看>>
UNET学习笔记2 - 高级API(HLAPI)
查看>>
"ORA-00942: 表或视图不存在 "的原因和解决方法[转]
查看>>
Oauth支持的5类 grant_type 及说明
查看>>
C#中用DateTime的ParseExact方法解析日期时间(excel中使用系统默认的日期格式)
查看>>
W3100SM-S 短信猫代码发送 上
查看>>
netty接收大文件的方法
查看>>
软件工程设计之四则运算
查看>>
SpringMVC @ResponseBody 406
查看>>
Partial Tree UVALive - 7190(完全背包)
查看>>
Happy Number
查看>>
vue中ESlint报错
查看>>
0816 1459 json & pickle ,目录导入,目录规范
查看>>
HDU 1398
查看>>
如何恢复oracle中已删除的表
查看>>
GridView中的CheckBox选中 (JQuery)
查看>>