日韩欧美人妻无码精品白浆,www.大香蕉久久网,狠狠的日狠狠的操,日本好好热在线观看

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#常見的四種經(jīng)典查找算法


2024年10月24日 9:39 本文熱度 1769

C#二分查找算法

簡介

二分查找算法是一種在有序數(shù)組中查找特定元素的搜索算法。

代碼實(shí)現(xiàn)

public class 二分查找算法
    {
        /// <summary>
        /// 二分查找算法
        /// </summary>
        /// <param name="arr">arr是已排序的數(shù)組</param>
        /// <param name="target">target是要查找的目標(biāo)值</param>
        /// <returns>目標(biāo)值在數(shù)組中的索引,如果未找到則返回-1</returns>
        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)
                {
                    // 如果中間元素等于目標(biāo)值
                    return mid; // 查找成功,返回索引
                }
                else if (arr[mid] < target)
                {
                    // 如果目標(biāo)值小于中間元素,則在左半部分查找
                    left = mid + 1;
                }
                else
                {
                    // 如果目標(biāo)值大于中間元素,則在右半部分查找
                    right = mid - 1;
                }
            }
            // 未找到 target,返回-1
            return -1;
        }
        public static void BinarySearchRun()
        {
            int[] arr = { 135711131719232931374143475359 }; //注意:這里的數(shù)組是已排序的數(shù)組
            int target = 31//需要要查找的目標(biāo)值
            int result = BinarySearch(arr, target); //調(diào)用二分查找方法
            if (result == -1)
            {
                Console.WriteLine("元素未找到");
            }
            else
            {
                Console.WriteLine($"元素找到,索引為:{result},值為:{arr[result]}");
            }
        }
    }

C#線性查找算法

簡介

線性查找算法是一種簡單的查找算法,用于在一個數(shù)組或列表中查找一個特定的元素。它從數(shù)組的第一個元素開始,逐個檢查每個元素,直到找到所需的元素或搜索完整個數(shù)組。線性查找的時間復(fù)雜度為O(n),其中n是數(shù)組中的元素數(shù)量。

代碼實(shí)現(xiàn)

  public static void LinearSearchRun()
        {
            int[] arr = { 234104050100778899 };
            int target = 100;
            int result = LinearSearch(arr, target);
            // 輸出結(jié)果
            if (result == -1)
            {
                Console.WriteLine("元素未找到");
            }
            else
            {
                Console.WriteLine($"元素在索引 {result} 處找到,index = {result}");
            }
        }
        /// <summary>
        /// 線性查找函數(shù)
        /// </summary>
        /// <param name="arr">arr</param>
        /// <param name="target">target</param>
        /// <returns></returns>
        public static int LinearSearch(int[] arr, int target)
        {
            // 遍歷數(shù)組
            for (int i = 0; i < arr.Length; i++)
            {
                // 如果找到目標(biāo)值,返回其索引
                if (arr[i] == target)
                {
                    return i;
                }
            }
            // 如果沒有找到,則返回-1
            return -1;
        }

C#二叉搜索樹算法

簡介

二叉搜索樹(Binary Search Tree,簡稱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)); // 輸出: True
            Console.WriteLine("Search for 25: " + bst.Search(25)); // 輸出: False
            Console.WriteLine("\n");
            // 刪除某個值
            bst.Delete(50);
            Console.WriteLine("刪除50后:");
            bst.InorderTraversal();
        }
    }
    /// <summary>
    /// 定義二叉搜索樹的節(jié)點(diǎn)結(jié)構(gòu)
    /// </summary>
    public class TreeNode
    {
        public int Value;
        public TreeNode Left;
        public TreeNode Right;
        public TreeNode(int value)
        {
            Value = value;
            Left = null;
            Right = null;
        }
    }
    /// <summary>
    /// 定義二叉搜索樹類
    /// </summary>
    public class BinarySearchTree
    {
        private TreeNode root;
        public BinarySearchTree()
        {
            root = null;
        }
        #region 插入節(jié)點(diǎn)
        /// <summary>
        /// 插入新值到二叉搜索樹中
        /// </summary>
        /// <param name="value">value</param>
        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
            {
                //值已經(jīng)存在于樹中,不再插入
                return;
            }
        }
        #endregion
        #region 查找節(jié)點(diǎn)
        /// <summary>
        /// 查找某個值是否存在于二叉搜索樹中
        /// </summary>
        /// <param name="value">value</param>
        /// <returns></returns>
        public bool Search(int value)
        {
            return SearchRec(root, value);
        }
        private bool SearchRec(TreeNode node, int value)
        {
            // 如果當(dāng)前節(jié)點(diǎn)為空,表示未找到目標(biāo)值
            if (node == null)
            {
                return false;
            }
            // 如果找到目標(biāo)值,返回true
            if (node.Value == value)
            {
                return true;
            }
            // 遞歸查找左子樹或右子樹
            if (value < node.Value)
            {
                return SearchRec(node.Left, value);
            }
            else
            {
                return SearchRec(node.Right, value);
            }
        }
        #endregion
        #region 中序遍歷
        /// <summary>
        /// 中序遍歷(打印有序數(shù)組)
        /// </summary>
        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)
        /// <summary>
        /// 刪除某個值
        /// </summary>
        /// <param name="val">val</param>
        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
            {
                // 節(jié)點(diǎn)有兩個子節(jié)點(diǎn)
                if (node.Left != null && node.Right != null)
                {
                    // 使用右子樹中的最小節(jié)點(diǎn)替換當(dāng)前節(jié)點(diǎn)
                    TreeNode minNode = FindMin(node.Right);
                    node.Value = minNode.Value;
                    node.Right = DeleteNode(node.Right, minNode.Value);
                }
                // 節(jié)點(diǎn)有一個子節(jié)點(diǎn)或沒有子節(jié)點(diǎn)
                else
                {
                    TreeNode? temp = node.Left != null ? node.Left : node.Right;
                    node = temp;
                }
            }
            return node;
        }
        /// <summary>
        /// 找到樹中的最小節(jié)點(diǎn)
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private TreeNode FindMin(TreeNode node)
        {
            while (node.Left != null)
            {
                node = node.Left;
            }
            return node;
        }
        #endregion
    }
}

C#哈希查找算法

簡介

哈希查找算法是一種高效的查找算法,通過將鍵值映射到哈希表中的位置來實(shí)現(xiàn)快速訪問。在C#中,哈希查找通常通過哈希表(Hashtable)或字典(Dictionary)來實(shí)現(xiàn)。

代碼實(shí)現(xiàn)

 public class 哈希查找算法
    {
        /// <summary>
        /// 哈希查找函數(shù)
        /// </summary>
        /// <param name="target">target</param>
        public static void HashSearchFunctionRun(int target)
        {
            //創(chuàng)建一個字典來存儲鍵值對
            var dic = new Dictionary<int, string>();
            dic.Add(1"one");
            dic.Add(2"two");
            dic.Add(3"three");
            //查找目標(biāo)值是否在Dictionary中存在
            //TryGetValue方法可以返回一個bool值和值,如果找到了目標(biāo)值,則返回true和對應(yīng)的值,否則返回false和默認(rèn)值
            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 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場、車隊、財務(wù)費(fèi)用、相關(guān)報表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

欧亚国产成人精品视频播放器| 综合人妻中文字幕一区| 激情综合网色播五月天婷婷| 兄弟强外女高潮动态图一区二区| 足交后入一区二区| 久久精品香蕉网| 国产三级精品三级在线观看| 小骚逼想被艹逼av| 性高潮久久| 广宗县| 国产无码丁香五月| 不要在操B国产| 亚洲av一级片| 麻豆专区一区二区三区四区五区| 清纯日韩欧美小说在线观看| 偷拍欧美不卡| 高清性色生活| 女女成人视频一区| 九几九九九美日韩九九九九九| 色悠久久五月| 色欲久久国产一区| 嗯嗯啊不行视频| 精品国产久久久Av无码成人| 久久久久99成人殴美精品| 欧美丝袜一区精品久久| 日韩式女人操男人| 亚洲天堂精品在线高清| 日本二区不卡免费| 免费亚欧无线AV| 安新县| 张开腿让我操一区二区三区| 久久丝袜国产露脸国语懂色| 久久久久久国产美女| 大香蕉免费大香蕉免费| 三级片免费看中文字| 久久按摩99| 日本无码精品一区二区| 欧美三级色色色| 色天堂网| 成人午夜福利不卡福利不卡| 一区二区精品人妻|