zoukankan      html  css  js  c++  java
  • C# 二分查询

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

    image

  • 相关阅读:
    汉语编程
    第一次作业
    个人总结
    psp表格
    第三次个人作业——用例图设计
    第二次结对作业
    第一次结对作业
    第二次个人编程作业
    第一次编程作业
    第一次博客作业
  • 原文地址:https://www.cnblogs.com/plateFace/p/5196146.html
Copyright © 2011-2022 走看看