算法 - 折半查找(C#)
2009-09-30 14:53
267 查看
递归实现:
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chimomo's Company" file="Program.cs">
// Respect the work.
// </copyright>
// <summary>
// The binary search (recursive).
// [折半查找的前提]:
// 1、待查找序列必须采用顺序存储结构。
// 2、待查找序列必须是按关键字大小有序排列。
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace CSharpLearning
{
using System;
/// <summary>
/// The program.
/// </summary>
internal class Program
{
/// <summary>
/// Entry point into console application.
/// </summary>
public static void Main()
{
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Console.WriteLine(BinarySearch(a, 6, 0, 9));
}
/// <summary>
/// 在下界为low,上界为high的有序数组a中折半查找数据元素x(递归查找)。
/// </summary>
/// <param name="a">
/// 待查找数组。
/// </param>
/// <param name="x">
/// 目标元素。
/// </param>
/// <param name="low">
/// 数组元素下标的下界。
/// </param>
/// <param name="high">
/// 数组元素下标的上界。
/// </param>
/// <returns>
/// 若查找到目标元素则返回该目标元素在数组中的下标;否则返回-1。
/// </returns>
private static int BinarySearch(int[] a, int x, int low, int high)
{
if (low > high)
{
return -1;
}
int mid = (low + high) / 2;
if (x == a[mid])
{
return mid;
}
return x < a[mid] ? BinarySearch(a, x, low, mid - 1) : BinarySearch(a, x, mid + 1, high);
}
}
}
// Output:
/*
5
*/时间复杂度:O(log2n)
非递归实现:
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chimomo's Company" file="Program.cs">
// Respect the work.
// </copyright>
// <summary>
// The binary search (not recursive).
// [折半查找的前提]:
// 1、待查找序列必须采用顺序存储结构。
// 2、待查找序列必须是按关键字大小有序排列。
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace CSharpLearning
{
using System;
/// <summary>
/// The program.
/// </summary>
internal class Program
{
/// <summary>
/// Entry point into console application.
/// </summary>
public static void Main()
{
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Console.WriteLine(BinarySearch(a, 6, 9));
}
/// <summary>
/// 在长度为n的有序数组a中查找值为key的元素(非递归查找)。
/// </summary>
/// <param name="a">
/// 待查找数组。
/// </param>
/// <param name="key">
/// 目标元素。
/// </param>
/// <param name="n">
/// 数组长度。
/// </param>
/// <returns>
/// 若查找到目标元素则返回该目标元素在数组中的下标;否则返回-1。
/// </returns>
private static int BinarySearch(int[] a, int key, int n)
{
int low = 0;
int high = n - 1;
while (low <= high)
{
int mid = (low + high) / 2;
if (a[mid] == key)
{
return mid;
}
if (a[mid] < key)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
}
}
// Output:
/*
5
*/时间复杂度:O(log2n)
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chimomo's Company" file="Program.cs">
// Respect the work.
// </copyright>
// <summary>
// The binary search (recursive).
// [折半查找的前提]:
// 1、待查找序列必须采用顺序存储结构。
// 2、待查找序列必须是按关键字大小有序排列。
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace CSharpLearning
{
using System;
/// <summary>
/// The program.
/// </summary>
internal class Program
{
/// <summary>
/// Entry point into console application.
/// </summary>
public static void Main()
{
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Console.WriteLine(BinarySearch(a, 6, 0, 9));
}
/// <summary>
/// 在下界为low,上界为high的有序数组a中折半查找数据元素x(递归查找)。
/// </summary>
/// <param name="a">
/// 待查找数组。
/// </param>
/// <param name="x">
/// 目标元素。
/// </param>
/// <param name="low">
/// 数组元素下标的下界。
/// </param>
/// <param name="high">
/// 数组元素下标的上界。
/// </param>
/// <returns>
/// 若查找到目标元素则返回该目标元素在数组中的下标;否则返回-1。
/// </returns>
private static int BinarySearch(int[] a, int x, int low, int high)
{
if (low > high)
{
return -1;
}
int mid = (low + high) / 2;
if (x == a[mid])
{
return mid;
}
return x < a[mid] ? BinarySearch(a, x, low, mid - 1) : BinarySearch(a, x, mid + 1, high);
}
}
}
// Output:
/*
5
*/时间复杂度:O(log2n)
非递归实现:
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chimomo's Company" file="Program.cs">
// Respect the work.
// </copyright>
// <summary>
// The binary search (not recursive).
// [折半查找的前提]:
// 1、待查找序列必须采用顺序存储结构。
// 2、待查找序列必须是按关键字大小有序排列。
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace CSharpLearning
{
using System;
/// <summary>
/// The program.
/// </summary>
internal class Program
{
/// <summary>
/// Entry point into console application.
/// </summary>
public static void Main()
{
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Console.WriteLine(BinarySearch(a, 6, 9));
}
/// <summary>
/// 在长度为n的有序数组a中查找值为key的元素(非递归查找)。
/// </summary>
/// <param name="a">
/// 待查找数组。
/// </param>
/// <param name="key">
/// 目标元素。
/// </param>
/// <param name="n">
/// 数组长度。
/// </param>
/// <returns>
/// 若查找到目标元素则返回该目标元素在数组中的下标;否则返回-1。
/// </returns>
private static int BinarySearch(int[] a, int key, int n)
{
int low = 0;
int high = n - 1;
while (low <= high)
{
int mid = (low + high) / 2;
if (a[mid] == key)
{
return mid;
}
if (a[mid] < key)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
}
}
// Output:
/*
5
*/时间复杂度:O(log2n)
相关文章推荐
- 算法 - 折半查找(C#)
- C# AES和MD5密码的算法,本人测试:CS/BS解密的结果一致。
- 算法(第四版)C# 习题题解——1.3.49 用 6 个栈实现一个 O(1) 队列
- 解析C#彩色图像灰度化算法的实现代码详解
- c# 删除 DataTable 里面空行的算法
- 各常见排序算法实践 C#
- [翻译]C#数据结构与算法 – 第四章 基本查找算法
- 远程桌面,RDP文件密码加密、解密算法(C#)
- 折半查找(C#数据结构学习六)
- C# 算法 之 排序算法
- C#组合算法 (2)
- C#算法大全-2-Algorithm Gossip: 费式数列
- 经典算法-C#四种排序算法
- 不足80行,一种清晰而又简单通用的分页算法,你有这么想过吗?C#版
- 数据算法之折半查找(binSearch)的Java实现
- 【年终分享】彩票数据预测算法(一):离散型马尔可夫链模型实现【附C#代码】
- 插入排序算法--直接插入算法,折半排序算法,希尔排序算法(C#实现)
- 农历天干地支算法源代码大全(javascript、vbscript、C#、flash、C++、C等等)
- c#迭代算法
- C# 算法 之 查找算法