数据结构和算法系列10 五大查找之二分查找

来源:互联网 发布:ai算法 编辑:程序博客网 时间:2024/06/07 03:23

二分查找也属于顺序表查找范围,二分查找也称为折半查找。二分查找(有序)的时间复杂度为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("********************二分查找********************\n");            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("\n**********展示结果**********\n");            if (list != null && list.Count > 0)            {                foreach (var item in list)                {                    Console.Write("{0} ", item);                }            }            Console.WriteLine("\n**********展示完毕**********\n");        }    }}
复制代码

程序输出结果如图:

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 20typedef 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("\n**********展示结果**********\n");    for (i=0;i<seqList->length;i++)    {        printf("%d ",seqList->data[i]);    }    printf("\n**********展示完毕**********\n");}#define N 9void 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版)***************\n");    Display(&seqList);    j=BinarySearch(&seqList,40);    if (j!=-1) printf("40在列表中的位置是:%d\n",j);    else printf("对不起,没有找到该元素!");    getchar();}
复制代码

程序输出结果如图:

ds36

0 0
原创粉丝点击