using System; class BinarySearch { public int search(int[] A, int p, int q, int key) { int result = -1; if (p >= q && key == A[p]) { result = p; } else { int k = (p+q)/2; if (key == A[k]) result = k; else result = (key < A[k]) ? search(A, p, k-1, key) : search(A, k+1, q, key); } return result; } } class Program { public static void Main() { Random rnd = new Random(); int[] data = new int[10]; for (int i=0; i<10; i++) { data[i] = i; Console.Write("{0}\t",data[i]); } Console.WriteLine(); BinarySearch s = new BinarySearch(); int t = s.search(data, 0, 9, 5); Console.Write(t); } }
using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using System.Collections; using System.IO; using System.Xml;namespace CSharpTest { class B { static void Main(string[] args) { int[] arrint ={ 1, 2, 3, 4, 5 }; Console.WriteLine(find(3, arrint)); } public static int find(int n, int[] arrint) { int begin = 0, end = arrint.Length, mid; while (begin <= end) { mid = (begin + end) / 2; if (arrint[mid] == n) return mid; else if (arrint[mid] > n) { end = mid; } else begin = mid; } return -1; } } }
基础的算法二分查找(递归) static int BinarySearch(int[] array, int targetValue, int beginIndex, int endIndex) { if (beginIndex > endIndex) { return -1; } int index = (beginIndex + endIndex) / 2; if (targetValue == array[index]) { return index; } else if (targetValue < array[index]) { return BinarySearch(array, targetValue, beginIndex, index - 1); } else { return BinarySearch(array, targetValue, index + 1, endIndex); } }二分查找(非递归) static int BinarySearch(int[] array, int targetValue, int beginIndex, int endIndex) { if (array == null) { throw new ArgumentNullException("..."); } if (beginIndex < 0 || endIndex > array.Length - 1 || beginIndex > endIndex) { throw new ArgumentException("..."); } int result = -1; int index = (beginIndex + endIndex) / 2; while (beginIndex <= endIndex) { if (targetValue == array[index]) { result = index; break; } else if (targetValue < array[index]) { endIndex = index - 1; } else { beginIndex = index + 1; } index = (beginIndex + endIndex) / 2; } return result; }
引用: 折半查找法也称为二分查找法,它充分利用了元素间的次序关系,采用分治策略,可在最坏的情况下用O(log n)完成搜索任务。它的基本思想是,将n个元素分成个数大致相同的两半,取a[n/2]与欲查找的x作比较,如果x=a[n/2]则找到x,算法终止。如果x<a[n/2],则我们只要在数组a的左半部继续搜索x(这里假设数组元素呈升序排列)。如果x>a[n/2],则我们只要在数组a的右半部继续搜索x。二分搜索法的应用极其广泛,而且它的思想易于理解,但是要写一个正确的二分搜索算法也不是一件简单的事。第一个二分搜索算法早在1946年就出现了,但是第一个完全正确的二分搜索算法直到1962年才出现。Bentley在他的著作《Writing Correct Programs》中写道,90%的计算机专家不能在2小时内写出完全正确的二分搜索算法。问题的关键在于准确地制定各次查找范围的边界以及终止条件的确定,正确地归纳奇偶数的各种情况,其实整理后可以发现它的具体算法是很直观的,我们可用C++描述如下: template<class Type> int BinarySearch(Type a[],const Type& x,int n) { int left=0; int right=n-1; while(left<=right){ int middle=(left+right)/2; if (x==a[middle]) return middle; if (x>a[middle]) left=middle+1; else right=middle-1; } return -1; }
using System;
class BinarySearch
{
public int search(int[] A, int p, int q, int key)
{
int result = -1;
if (p >= q && key == A[p])
{
result = p;
}
else
{
int k = (p+q)/2;
if (key == A[k])
result = k;
else
result = (key < A[k]) ? search(A, p, k-1, key) : search(A, k+1, q, key);
}
return result;
}
}
class Program
{
public static void Main()
{
Random rnd = new Random();
int[] data = new int[10];
for (int i=0; i<10; i++)
{
data[i] = i;
Console.Write("{0}\t",data[i]);
}
Console.WriteLine();
BinarySearch s = new BinarySearch();
int t = s.search(data, 0, 9, 5);
Console.Write(t);
}
}
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.IO;
using System.Xml;namespace CSharpTest
{ class B
{
static void Main(string[] args)
{
int[] arrint ={ 1, 2, 3, 4, 5 };
Console.WriteLine(find(3, arrint));
}
public static int find(int n, int[] arrint)
{
int begin = 0, end = arrint.Length, mid;
while (begin <= end)
{
mid = (begin + end) / 2;
if (arrint[mid] == n) return mid;
else if (arrint[mid] > n)
{
end = mid;
}
else
begin = mid;
}
return -1; } }
}
static int BinarySearch(int[] array, int targetValue, int beginIndex, int endIndex)
{
if (beginIndex > endIndex)
{
return -1;
} int index = (beginIndex + endIndex) / 2;
if (targetValue == array[index])
{
return index;
}
else if (targetValue < array[index])
{
return BinarySearch(array, targetValue, beginIndex, index - 1);
}
else
{
return BinarySearch(array, targetValue, index + 1, endIndex);
}
}二分查找(非递归)
static int BinarySearch(int[] array, int targetValue, int beginIndex, int endIndex)
{
if (array == null)
{
throw new ArgumentNullException("...");
}
if (beginIndex < 0 || endIndex > array.Length - 1 || beginIndex > endIndex)
{
throw new ArgumentException("...");
} int result = -1;
int index = (beginIndex + endIndex) / 2;
while (beginIndex <= endIndex)
{
if (targetValue == array[index])
{
result = index;
break;
}
else if (targetValue < array[index])
{
endIndex = index - 1;
}
else
{
beginIndex = index + 1;
}
index = (beginIndex + endIndex) / 2;
}
return result;
}
折半查找法也称为二分查找法,它充分利用了元素间的次序关系,采用分治策略,可在最坏的情况下用O(log n)完成搜索任务。它的基本思想是,将n个元素分成个数大致相同的两半,取a[n/2]与欲查找的x作比较,如果x=a[n/2]则找到x,算法终止。如果x<a[n/2],则我们只要在数组a的左半部继续搜索x(这里假设数组元素呈升序排列)。如果x>a[n/2],则我们只要在数组a的右半部继续搜索x。二分搜索法的应用极其广泛,而且它的思想易于理解,但是要写一个正确的二分搜索算法也不是一件简单的事。第一个二分搜索算法早在1946年就出现了,但是第一个完全正确的二分搜索算法直到1962年才出现。Bentley在他的著作《Writing Correct Programs》中写道,90%的计算机专家不能在2小时内写出完全正确的二分搜索算法。问题的关键在于准确地制定各次查找范围的边界以及终止条件的确定,正确地归纳奇偶数的各种情况,其实整理后可以发现它的具体算法是很直观的,我们可用C++描述如下: template<class Type> int BinarySearch(Type a[],const Type& x,int n) { int left=0; int right=n-1; while(left<=right){ int middle=(left+right)/2; if (x==a[middle]) return middle; if (x>a[middle]) left=middle+1; else right=middle-1; } return -1; }
模板函数BinarySearch在a[0]<=a[1]<=...<=a[n-1]共n个升序排列的元素中搜索x,找到x时返回其在数组中的位置,否则返回-1。容易看出,每执行一次while循环,待搜索数组的大小减少一半,因此整个算法在最坏情况下的时间复杂度为O(log n)。在数据量很大的时候,它的线性查找在时间复杂度上的优劣一目了然。