zoukankan      html  css  js  c++  java
  • 数据结构和算法系列10 五大查找之二分查找

    二分查找也属于顺序表查找范围,二分查找也称为折半查找。二分查找(有序)的时间复杂度为O(LogN)。

    那么什么是二分查找呢?二分查找的基本思想是, 在有序表中,取中间记录作为比较对象,若给定值与中间记录的关键字相等,则查找成功;若给定值小于中间记录的关键字,则在中间记录的左半区继续查找;若给定值大于中间记录的关键字,则在中间记录的右半区继续查找。不断重复上述过程,直到找到为止。

    从二分查找的定义我们可以看出,使用二分查找有两个前提条件:

    1,待查找的列表必须有序。

    2,必须使用线性表的顺序存储结构来存储数据。

    下面是实现代码。

    C#版:

    namespace BinarySearch.CSharp
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<int> list = new List<int> { 10,20,30,40,50,60,70,80,90 };
    
                Console.WriteLine("********************二分查找********************
    ");
                Display(list);
                int result = BinarySearch(list, 40);
                if (result != -1) Console.WriteLine("40在列表中的位置是:{0}", result);
                else Console.WriteLine("对不起,列表中不存在该元素!");
    
                Console.ReadKey();
            }
    
            /// <summary>
            /// 二分查找
            /// </summary>
            /// <param name="list">查找表</param>
            /// <param name="key">给定值</param>
            /// <returns>给定值在列表中的位置</returns>
            public static int BinarySearch(List<int> list, int key)
            {
                int low = 0;
                int high = list.Count - 1;
    
                while (low <= high)
                {
                    int middle = (low + high) / 2;
    
                    //判断中间记录是否与给定值相等
                    if (list[middle] == key)
                    {
                        return middle;
                    }
                    else
                    {
                        //在中间记录的左半区查找
                        if (list[middle] > key) high = middle - 1;
    
                        //在中间记录的右半区查找
                        else low = middle + 1;
                    }
                }
    
                //没有找到(查找失败)
                return -1;
            }
    
            private static void Display(IList<int> list)
            {
                Console.WriteLine("
    **********展示结果**********
    ");
    
                if (list != null && list.Count > 0)
                {
                    foreach (var item in list)
                    {
                        Console.Write("{0} ", item);
                    }
                }
                Console.WriteLine("
    **********展示完毕**********
    ");
            }
        }
    }

    程序输出结果如图:

    ds35

     

    C语言版:

    /*包含头文件*/
    #include "stdio.h"
    #include "stdlib.h"   
    #include "io.h"
    #include "math.h" 
    #include "time.h"
    
    #define OK 1
    #define ERROR 0
    #define TRUE 1
    #define FALSE 0
    #define MAXSIZE 20
    
    typedef int Status; 
    typedef struct
    {
        int data[MAXSIZE];
        int length;    
    }SeqList;
    
    /*二分查找算法(折半查找)*/
    int BinarySearch(SeqList *seqList,int key)
    {
        /*下限*/
        int low=0;
    
        /*上限*/
        int high=seqList->length-1;
    
        while(low<=high) /*注意下限可以与上限重合的*/
        {
            int middle=(low+high)/2;
    
            /*判断中间记录是否与给定值相等*/
            if (seqList->data[middle]==key)
            {
                return middle;
            }
            else
            {
                /*缩小上限*/
                if (seqList->data[middle]>key) high=middle-1;
    
                /*扩大下限*/
                else low=middle+1;
            }
        }
    
        /*没有找到*/
        return -1;
    }
    
    
    /*打印结果*/
    void Display(SeqList *seqList)
    {
        int i;
        printf("
    **********展示结果**********
    ");
    
        for (i=0;i<seqList->length;i++)
        {
            printf("%d ",seqList->data[i]);
        }
    
        printf("
    **********展示完毕**********
    ");
    }
    
    #define N 9
    void main()
    {
        int i,j;
        SeqList seqList;
    
        //定义数组和初始化SeqList
        int d[N]={10,20,30,40,50,60,70,80,90};
    
        for (i=0;i<N;i++)
        {
            seqList.data[i]=d[i];
        }
        seqList.length=N;
    
        printf("***************二分查找(C版)***************
    ");
        Display(&seqList);
        j=BinarySearch(&seqList,40);
        if (j!=-1) printf("40在列表中的位置是:%d
    ",j);
        else printf("对不起,没有找到该元素!");
    
        getchar();
    }

    程序输出结果如图:

    ds36

  • 相关阅读:
    php 采集程序
    15个实用的PHP正则表达式
    jQuery Mobile优秀网站及资源汇总
    连接ORalce数据库
    Windows Mobile如何观看Webcast
    解决wpf中使用WinformsHost显示滚动条的问题
    利用反射调用类及其属性和方法
    一些实用的vs.net2008 快捷键(已验证)
    WW插件研究
    微软今天公布了下一代Visual Studio和.NET Framework开发工具和平台,该平台将被命名的Visual Studio 2010和.Net Framework 4.0.
  • 原文地址:https://www.cnblogs.com/mcgrady/p/3268923.html
Copyright © 2011-2022 走看看