AXzhz

专注ASP.NET!
        写软件的都是小姐,用软件的都是大爷。真TMD说的正确!
                嫖妓不给钱,反说被强奸!___中国共享软件的无奈!
读小学的时候大学不要钱,读大学了小学不要钱。
没工作时国家有分福利房,工作了后全是高价房!!

                        此软件能有效的破解QQ密码。(注:破解QQ密码是违法行为,请不要将软件用于违法行为)
“安得广厦千万间,大庇天下寒士俱欢颜,风雨不动安如山。”1200多年前,诗人杜甫的理想,如今被一帮享受着电脑和互联网带来的快捷生活方式的受过高等教育的大孩子憧憬着。

导航

二分查找的递归算法(附源码)_AX

【序】
  有些东西,总觉得很简单,但久了又忘,于是写到这里,用时Copy一下.

【思路】
在数组两边放上索引标识,表示要查询的数字在这两个索引表示的数组成员值范围内,然后不断缩小这个范围(折半缩小)

【源码】斧头帮少帮主

 1using System;
 2
 3namespace BinarySearch
 4{
 5    /// <summary>
 6    /// 二分搜索法,Made by AX。
 7    /// </summary>

 8    public class Search
 9    {
10        public static void Main()
11        {
12            int searchKey=8;                //要在数组中查找的数字
13            int[] scores={2,4,6,8,10,12};    //数组
14            int low=0;                        //数组最小索引
15            int high=scores.Length-1;        //数组最大索引
16            int searchResult=StartSearch(scores,searchKey,low,high);
17            Console.WriteLine("要查询的数的索引为:"+searchResult);
18            Console.ReadLine();
19        }

20        public static int StartSearch(int[] arr ,int key,int low,int high)
21        {            
22            if(low>high)
23            {
24                return -1;
25            }

26            else
27            {
28                //四舍五入方法,结果为6.0
29                //double AX=Math.Round(5.5);
30                //第一次执行(0+5)/2=2                
31                int mid=(low+high)/2;
32                if(arr[mid]==key)
33                {
34                    return mid;
35                }

36                else if(key<arr[mid])
37                {
38                    return StartSearch(arr,key,low,mid-1);
39                }

40                else
41                {
42                    return StartSearch(arr,key,mid+1,high);
43                }

44            }

45        }

46    }

47}

48

posted on 2006-12-06 15:09  斧头帮少帮主  阅读(5178)  评论(0)    收藏  举报

Google
 
站内搜索:        
园内搜索:
金山词霸: