程序开发中常用的十种算法,你用过几种?

开发 后端
当编写程序时,了解和使用不同的算法对解决问题至关重要。以下是C#中常用的10种算法,每个算法都伴随着示例代码和详细说明。

1、冒泡排序 (Bubble Sort):

冒泡排序是一种简单的比较排序算法,它多次遍历数组,将较大的元素逐渐浮动到数组的末尾。

public static void BubbleSort(int[] arr)
{
    int n = arr.Length;
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = 0; j < n - i - 1; j++)
        {
            if (arr[j] > arr[j + 1])
            {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

2、快速排序 (Quick Sort):

快速排序是一种高效的分治排序算法,它通过选择一个基准元素并将数组分为较小和较大的两部分来进行排序。

public static void QuickSort(int[] arr, int low, int high)
{
    if (low < high)
    {
        int partitionIndex = Partition(arr, low, high);
        QuickSort(arr, low, partitionIndex - 1);
        QuickSort(arr, partitionIndex + 1, high);
    }
}

public static int Partition(int[] arr, int low, int high)
{
    int pivot = arr[high];
    int i = low - 1;

    for (int j = low; j < high; j++)
    {
        if (arr[j] < pivot)
        {
            i++;
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }

    int swap = arr[i + 1];
    arr[i + 1] = arr[high];
    arr[high] = swap;

    return i + 1;
}

3、合并排序 (Merge Sort):

合并排序是一种稳定的分治排序算法,它将数组分成两半,分别排序后再合并。

public static void MergeSort(int[] arr)
{
    int n = arr.Length;
    if (n > 1)
    {
        int mid = n / 2;
        int[] left = new int[mid];
        int[] right = new int[n - mid];

        for (int i = 0; i < mid; i++)
            left[i] = arr[i];
        for (int i = mid; i < n; i++)
            right[i - mid] = arr[i];

        MergeSort(left);
        MergeSort(right);

        int i = 0, j = 0, k = 0;
        while (i < mid && j < (n - mid))
        {
            if (left[i] < right[j])
                arr[k++] = left[i++];
            else
                arr[k++] = right[j++];
        }
        while (i < mid)
            arr[k++] = left[i++];
        while (j < (n - mid))
            arr[k++] = right[j++];
    }
}

4、二分查找 (Binary Search):

二分查找是一种高效的查找算法,它要求在有序数组中查找特定元素。

public static int BinarySearch(int[] arr, int target)
{
    int low = 0, high = arr.Length - 1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (arr[mid] == target)
            return mid;
        else if (arr[mid] < target)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return -1;
}

5、深度优先搜索 (Depth-First Search, DFS):

DFS 是一种图遍历算法,它从起始节点开始,沿着路径尽可能深入,然后返回并继续搜索。

using System;
using System.Collections.Generic;

public class Graph
{
    private int V;
    private List<int>[] adj;

    public Graph(int v)
    {
        V = v;
        adj = new List<int>[v];
        for (int i = 0; i < v; i++)
            adj[i] = new List<int>();
    }

    public void AddEdge(int v, int w)
    {
        adj[v].Add(w);
    }

    public void DFS(int v)
    {
        bool[] visited = new bool[V];
        DFSUtil(v, visited);
    }

    private void DFSUtil(int v, bool[] visited)
    {
        visited[v] = true;
        Console.Write(v + " ");

        foreach (var n in adj[v])
        {
            if (!visited[n])
                DFSUtil(n, visited);
        }
    }
}

6、广度优先搜索 (Breadth-First Search, BFS):

BFS 是一种图遍历算法,它从起始节点开始,逐层遍历,先访问所有相邻的节点,然后再逐层扩展。

using System;
using System.Collections.Generic;

public class Graph
{
    private int V;
    private List<int>[] adj;

    public Graph(int v)
    {
        V = v;
        adj = new List<int>[v];
        for (int i = 0; i < v; i++)
            adj[i] = new List<int>();
    }

    public void AddEdge(int v, int w)
    {
        adj[v].Add(w);
    }

    public void BFS(int s)
    {
        bool[] visited = new bool[V];

        Queue<int> queue = new Queue<int>();
        visited[s] = true;
        queue.Enqueue(s);

        while (queue.Count != 0)
        {
            s = queue.Dequeue();
            Console.Write(s + " ");

            foreach (var n in adj[s])
            {
                if (!visited[n])
                {
                    visited[n] = true;
                    queue.Enqueue(n);
                }
            }
        }
    }
}

7、Dijkstra算法:

Dijkstra算法是一种用于查找图中最短路径的算法。

public class Dijkstra
{
    private static int V = 9;

    private int MinDistance(int[] dist, bool[] sptSet)
    {
        int min = int.MaxValue;
        int minIndex = 0;

        for (int v = 0; v < V; v++)
        {
            if (!sptSet[v] && dist

[v] <= min)
            {
                min = dist[v];
                minIndex = v;
            }
        }

        return minIndex;
    }

    private void PrintSolution(int[] dist)
    {
        Console.WriteLine("Vertex \t Distance from Source");
        for (int i = 0; i < V; i++)
        {
            Console.WriteLine(i + " \t " + dist[i]);
        }
    }

    public void FindShortestPath(int[,] graph, int src)
    {
        int[] dist = new int[V];
        bool[] sptSet = new bool[V];

        for (int i = 0; i < V; i++)
        {
            dist[i] = int.MaxValue;
            sptSet[i] = false;
        }

        dist[src] = 0;

        for (int count = 0; count < V - 1; count++)
        {
            int u = MinDistance(dist, sptSet);

            sptSet[u] = true;

            for (int v = 0; v < V; v++)
            {
                if (!sptSet[v] && graph[u, v] != 0 && dist[u] != int.MaxValue && dist[u] + graph[u, v] < dist[v])
                {
                    dist[v] = dist[u] + graph[u, v];
                }
            }
        }

        PrintSolution(dist);
    }
}

8、最小生成树 (Minimum Spanning Tree, MST) - Prim算法:

Prim算法用于找到图的最小生成树,它从一个初始顶点开始,逐渐扩展生成树。

public class PrimMST
{
    private static int V = 5;

    private int MinKey(int[] key, bool[] mstSet)
    {
        int min = int.MaxValue;
        int minIndex = 0;

        for (int v = 0; v < V; v++)
        {
            if (!mstSet[v] && key[v] < min)
            {
                min = key[v];
                minIndex = v;
            }
        }

        return minIndex;
    }

    private void PrintMST(int[] parent, int[,] graph)
    {
        Console.WriteLine("Edge \t Weight");
        for (int i = 1; i < V; i++)
        {
            Console.WriteLine(parent[i] + " - " + i + " \t " + graph[i, parent[i]]);
        }
    }

    public void FindMST(int[,] graph)
    {
        int[] parent = new int[V];
        int[] key = new int[V];
        bool[] mstSet = new bool[V];

        for (int i = 0; i < V; i++)
        {
            key[i] = int.MaxValue;
            mstSet[i] = false;
        }

        key[0] = 0;
        parent[0] = -1;

        for (int count = 0; count < V - 1; count++)
        {
            int u = MinKey(key, mstSet);

            mstSet[u] = true;

            for (int v = 0; v < V; v++)
            {
                if (graph[u, v] != 0 && !mstSet[v] && graph[u, v] < key[v])
                {
                    parent[v] = u;
                    key[v] = graph[u, v];
                }
            }
        }

        PrintMST(parent, graph);
    }
}

9、最小生成树 (Minimum Spanning Tree, MST) - Kruskal算法:

Kruskal算法也用于找到图的最小生成树,它基于边的权重排序。

using System;
using System.Collections.Generic;

public class Graph
{
    private int V, E;
    private List<Edge> edges;

    public Graph(int v, int e)
    {
        V = v;
        E = e;
        edges = new List<Edge>(e);
    }

    public void AddEdge(int src, int dest, int weight)
    {
        edges.Add(new Edge(src, dest, weight));
    }

    public void KruskalMST()
    {
        edges.Sort();

        int[] parent = new int[V];
        int[] rank = new int[V];

        for (int i = 0; i < V; i++)
        {
            parent[i] = i;
            rank[i] = 0;
        }

        int i = 0;
        int e = 0;

        List<Edge> mst = new List<Edge>();

        while (e < V - 1)
        {
            Edge nextEdge = edges[i++];
            int x = Find(parent, nextEdge.src);
            int y = Find(parent, nextEdge.dest);

            if (x != y)
            {
                mst.Add(nextEdge);
                Union(parent, rank, x, y);
                e++;
            }
        }

        Console.WriteLine("Edges in Minimum Spanning Tree:");
        foreach (var edge in mst)
        {
            Console.WriteLine($"{edge.src} - {edge.dest} with weight {edge.weight}");
        }
    }

    private int Find(int[] parent, int i)
    {
        if (parent[i] == i)
            return i;
        return Find(parent, parent[i]);
    }

    private void Union(int[] parent, int[] rank, int x, int y)
    {
        int xRoot = Find(parent, x);
        int yRoot = Find(parent, y);

        if (rank[xRoot] < rank[yRoot])
            parent[xRoot] = yRoot;
        else if (rank[xRoot] > rank[yRoot])
            parent[yRoot] = xRoot;
        else
        {
            parent[yRoot] = xRoot;
            rank[xRoot]++;
        }
    }
}

public class Edge : IComparable<Edge>
{
    public int src, dest, weight;

    public Edge(int src, int dest, int weight)
    {
        this.src = src;
        this.dest = dest;
        this.weight = weight;
    }

    public int CompareTo(Edge other)
    {
        return weight - other.weight;
    }
}

10、Floyd-Warshall算法是一种用于解决所有点对最短路径的动态规划算法。

下面是C#中的Floyd-Warshall算法的实现示例:

using System;

class FloydWarshall
{
    private static int INF = int.MaxValue; // 代表无穷大的值

    public static void FindShortestPath(int[,] graph)
    {
        int V = graph.GetLength(0);

        // 创建一个二维数组dist,用于保存最短路径的长度
        int[,] dist = new int[V, V];

        // 初始化dist数组
        for (int i = 0; i < V; i++)
        {
            for (int j = 0; j < V; j++)
            {
                dist[i, j] = graph[i, j];
            }
        }

        // 逐个顶点考虑,如果经过k顶点路径比原路径短,就更新dist数组
        for (int k = 0; k < V; k++)
        {
            for (int i = 0; i < V; i++)
            {
                for (int j = 0; j < V; j++)
                {
                    if (dist[i, k] != INF && dist[k, j] != INF
                        && dist[i, k] + dist[k, j] < dist[i, j])
                    {
                        dist[i, j] = dist[i, k] + dist[k, j];
                    }
                }
            }
        }

        // 输出最短路径矩阵
        Console.WriteLine("最短路径矩阵:");
        for (int i = 0; i < V; i++)
        {
            for (int j = 0; j < V; j++)
            {
                if (dist[i, j] == INF)
                    Console.Write("INF\t");
                else
                    Console.Write(dist[i, j] + "\t");
            }
            Console.WriteLine();
        }
    }

    static void Main(string[] args)
    {
        int V = 4; // 顶点数
        int[,] graph = {
            {0, 5, INF, 10},
            {INF, 0, 3, INF},
            {INF, INF, 0, 1},
            {INF, INF, INF, 0}
        };

        FindShortestPath(graph);
    }
}

在这个示例中,我们使用Floyd-Warshall算法来计算给定图的最短路径矩阵。该算法通过考虑逐个中间顶点k,不断更新最短路径矩阵dist。最终,我们可以获得所有点对之间的最短路径长度。

责任编辑:姜华 来源: 今日头条
相关推荐

2023-05-17 12:33:11

AI人工智能

2022-04-17 23:09:07

深度学习人工智能芯片

2023-11-02 08:32:11

机器学习人工智能

2022-08-26 16:21:47

数据分析工具运营

2011-05-30 15:07:58

计算机语言

2024-10-30 16:39:45

2016-10-21 19:44:08

数据科学家算法

2023-11-29 14:34:15

机器学习统计学

2022-10-25 15:55:13

2022-09-25 23:34:42

算法回归算法机器学习

2022-02-14 22:04:50

浏览器Observer监听

2019-07-22 13:39:59

Python编辑器开发

2023-11-28 12:08:56

机器学习算法人工智能

2019-10-08 09:00:00

MySQL数据库

2022-02-21 23:12:21

软件开发网络安全互联网

2021-07-02 21:53:23

物联网开发编程语言开发

2022-07-29 10:31:33

算法Python

2022-03-28 20:57:31

私有属性class属性和方法

2022-05-17 09:14:50

聚类算法python

2024-04-28 11:22:18

点赞
收藏

51CTO技术栈公众号