C#二分查找算法
簡(jiǎn)介
二分查找算法是一種在有序數(shù)組中查找特定元素的搜索算法。
代碼實(shí)現(xiàn)
public class 二分查找算法
{
public static int BinarySearch(int[] arr, int target)
{
int left = 0;
int right = arr.Length - 1;
while (left <= right)
{
int mid = left + (right - left) / 2;
if (arr[mid] == target)
{
return mid;
}
else if (arr[mid] < target)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return -1;
}
public static void BinarySearchRun()
{
int[] arr = { 1, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59 };
int target = 31;
int result = BinarySearch(arr, target);
if (result == -1)
{
Console.WriteLine("元素未找到");
}
else
{
Console.WriteLine($"元素找到,索引為:{result},值為:{arr[result]}");
}
}
}
C#線性查找算法
簡(jiǎn)介
線性查找算法是一種簡(jiǎn)單的查找算法,用于在一個(gè)數(shù)組或列表中查找一個(gè)特定的元素。它從數(shù)組的第一個(gè)元素開始,逐個(gè)檢查每個(gè)元素,直到找到所需的元素或搜索完整個(gè)數(shù)組。線性查找的時(shí)間復(fù)雜度為O(n),其中n是數(shù)組中的元素?cái)?shù)量。
代碼實(shí)現(xiàn)
public static void LinearSearchRun()
{
int[] arr = { 2, 3, 4, 10, 40, 50, 100, 77, 88, 99 };
int target = 100;
int result = LinearSearch(arr, target);
if (result == -1)
{
Console.WriteLine("元素未找到");
}
else
{
Console.WriteLine($"元素在索引 {result} 處找到,index = {result}");
}
}
public static int LinearSearch(int[] arr, int target)
{
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == target)
{
return i;
}
}
return -1;
}
C#二叉搜索樹算法
簡(jiǎn)介
二叉搜索樹(Binary Search Tree,簡(jiǎn)稱BST)是一種節(jié)點(diǎn)有序排列的二叉樹數(shù)據(jù)結(jié)構(gòu)。
代碼實(shí)現(xiàn)
namespace HelloDotNetGuide.常見算法
{
public class 二叉搜索樹算法
{
public static void BinarySearchTreeRun()
{
var bst = new BinarySearchTree();
bst.Insert(50);
bst.Insert(30);
bst.Insert(20);
bst.Insert(40);
bst.Insert(70);
bst.Insert(60);
bst.Insert(80);
bst.Insert(750);
Console.WriteLine("中序遍歷(打印有序數(shù)組):");
bst.InorderTraversal();
Console.WriteLine("\n");
Console.WriteLine("Search for 40: " + bst.Search(40));
Console.WriteLine("Search for 25: " + bst.Search(25));
Console.WriteLine("\n");
bst.Delete(50);
Console.WriteLine("刪除50后:");
bst.InorderTraversal();
}
}
public class TreeNode
{
public int Value;
public TreeNode Left;
public TreeNode Right;
public TreeNode(int value)
{
Value = value;
Left = null;
Right = null;
}
}
public class BinarySearchTree
{
private TreeNode root;
public BinarySearchTree()
{
root = null;
}
#region 插入節(jié)點(diǎn)
public void Insert(int value)
{
if (root == null)
{
root = new TreeNode(value);
}
else
{
InsertRec(root, value);
}
}
private void InsertRec(TreeNode node, int value)
{
if (value < node.Value)
{
if (node.Left == null)
{
node.Left = new TreeNode(value);
}
else
{
InsertRec(node.Left, value);
}
}
else if (value > node.Value)
{
if (node.Right == null)
{
node.Right = new TreeNode(value);
}
else
{
InsertRec(node.Right, value);
}
}
else
{
return;
}
}
#endregion
#region 查找節(jié)點(diǎn)
public bool Search(int value)
{
return SearchRec(root, value);
}
private bool SearchRec(TreeNode node, int value)
{
if (node == null)
{
return false;
}
if (node.Value == value)
{
return true;
}
if (value < node.Value)
{
return SearchRec(node.Left, value);
}
else
{
return SearchRec(node.Right, value);
}
}
#endregion
#region 中序遍歷
public void InorderTraversal()
{
InorderTraversalRec(root);
}
private void InorderTraversalRec(TreeNode root)
{
if (root != null)
{
InorderTraversalRec(root.Left);
Console.WriteLine(root.Value);
InorderTraversalRec(root.Right);
}
}
#endregion
#region 刪除節(jié)點(diǎn)
public void Delete(int val)
{
root = DeleteNode(root, val);
}
private TreeNode DeleteNode(TreeNode node, int val)
{
if (node == null)
{
return null;
}
if (val < node.Value)
{
node.Left = DeleteNode(node.Left, val);
}
else if (val > node.Value)
{
node.Right = DeleteNode(node.Right, val);
}
else
{
if (node.Left != null && node.Right != null)
{
TreeNode minNode = FindMin(node.Right);
node.Value = minNode.Value;
node.Right = DeleteNode(node.Right, minNode.Value);
}
else
{
TreeNode? temp = node.Left != null ? node.Left : node.Right;
node = temp;
}
}
return node;
}
private TreeNode FindMin(TreeNode node)
{
while (node.Left != null)
{
node = node.Left;
}
return node;
}
#endregion
}
}
C#哈希查找算法
簡(jiǎn)介
哈希查找算法是一種高效的查找算法,通過將鍵值映射到哈希表中的位置來實(shí)現(xiàn)快速訪問。在C#中,哈希查找通常通過哈希表(Hashtable)或字典(Dictionary)來實(shí)現(xiàn)。
代碼實(shí)現(xiàn)
public class 哈希查找算法
{
public static void HashSearchFunctionRun(int target)
{
var dic = new Dictionary<int, string>();
dic.Add(1, "one");
dic.Add(2, "two");
dic.Add(3, "three");
string value;
if (dic.TryGetValue(target, out value))
{
Console.WriteLine("Found Data: " + value);
}
else
{
Console.WriteLine("Not Found Data.");
}
}
}
?
該文章在 2024/10/24 17:06:26 編輯過