1.二分查找
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace list 8 { 9 class Program 10 { 11 12 public static int BinarySearch(List<int> intList, int target) 13 { 14 //int? n = null; 15 int low = 0; 16 int high = intList.Count - 1; 17 18 while(low<=high) 19 { 20 int mid = (low + high) / 2; 21 if (intList[mid] == target) 22 return mid; 23 else if (intList[mid] > target) 24 high = mid-1; 25 else 26 low = mid+1; 27 } 28 29 return -999999; 30 } 31 32 static void Main(string[] args) 33 { 34 35 36 List<int> list = new List<int>(); 37 for (int i = 1; i < 10; i++) 38 list.Add(i); 39 40 int a=BinarySearch(list,1); 41 int b = list[a]; 42 43 Console.WriteLine("a.Index="+a+" , a.Value= "+b ); 44 Console.ReadKey(); 45 } 46 } 47 }