feat(csharp/sorting): add bucked_sort, counting_sort, radix_sort (#455)
* feat(csharp/sorting): add bucked_sort, counting_sort, radix_sort * use top level statements
This commit is contained in:
parent
b3640c53d1
commit
3b96ab6be9
@ -7,62 +7,61 @@
|
||||
using hello_algo.include;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_graph
|
||||
namespace hello_algo.chapter_graph;
|
||||
|
||||
public class graph_bfs
|
||||
{
|
||||
public class graph_bfs
|
||||
/* 广度优先遍历 BFS */
|
||||
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
||||
public static List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet)
|
||||
{
|
||||
/* 广度优先遍历 BFS */
|
||||
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
||||
public static List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet)
|
||||
// 顶点遍历序列
|
||||
List<Vertex> res = new List<Vertex>();
|
||||
// 哈希表,用于记录已被访问过的顶点
|
||||
HashSet<Vertex> visited = new HashSet<Vertex>() { startVet };
|
||||
// 队列用于实现 BFS
|
||||
Queue<Vertex> que = new Queue<Vertex>();
|
||||
que.Enqueue(startVet);
|
||||
// 以顶点 vet 为起点,循环直至访问完所有顶点
|
||||
while (que.Count > 0)
|
||||
{
|
||||
// 顶点遍历序列
|
||||
List<Vertex> res = new List<Vertex>();
|
||||
// 哈希表,用于记录已被访问过的顶点
|
||||
HashSet<Vertex> visited = new HashSet<Vertex>() { startVet };
|
||||
// 队列用于实现 BFS
|
||||
Queue<Vertex> que = new Queue<Vertex>();
|
||||
que.Enqueue(startVet);
|
||||
// 以顶点 vet 为起点,循环直至访问完所有顶点
|
||||
while (que.Count > 0)
|
||||
Vertex vet = que.Dequeue(); // 队首顶点出队
|
||||
res.Add(vet); // 记录访问顶点
|
||||
foreach (Vertex adjVet in graph.adjList[vet])
|
||||
{
|
||||
Vertex vet = que.Dequeue(); // 队首顶点出队
|
||||
res.Add(vet); // 记录访问顶点
|
||||
foreach (Vertex adjVet in graph.adjList[vet])
|
||||
if (visited.Contains(adjVet))
|
||||
{
|
||||
if (visited.Contains(adjVet))
|
||||
{
|
||||
continue; // 跳过已被访问过的顶点
|
||||
}
|
||||
que.Enqueue(adjVet); // 只入队未访问的顶点
|
||||
visited.Add(adjVet); // 标记该顶点已被访问
|
||||
continue; // 跳过已被访问过的顶点
|
||||
}
|
||||
que.Enqueue(adjVet); // 只入队未访问的顶点
|
||||
visited.Add(adjVet); // 标记该顶点已被访问
|
||||
}
|
||||
|
||||
// 返回顶点遍历序列
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化无向图 */
|
||||
Vertex[] v = Vertex.valsToVets(new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
|
||||
Vertex[][] edges = new Vertex[12][]
|
||||
{
|
||||
new Vertex[2] { v[0], v[1] }, new Vertex[2] { v[0], v[3] }, new Vertex[2] { v[1], v[2] },
|
||||
new Vertex[2] { v[1], v[4] }, new Vertex[2] { v[2], v[5] }, new Vertex[2] { v[3], v[4] },
|
||||
new Vertex[2] { v[3], v[6] }, new Vertex[2] { v[4], v[5] }, new Vertex[2] { v[4], v[7] },
|
||||
new Vertex[2] { v[5], v[8] }, new Vertex[2] { v[6], v[7] }, new Vertex[2] { v[7], v[8] }
|
||||
};
|
||||
|
||||
GraphAdjList graph = new GraphAdjList(edges);
|
||||
Console.WriteLine("\n初始化后,图为");
|
||||
graph.print();
|
||||
|
||||
/* 广度优先遍历 BFS */
|
||||
List<Vertex> res = graphBFS(graph, v[0]);
|
||||
Console.WriteLine("\n广度优先遍历(BFS)顶点序列为");
|
||||
Console.WriteLine(string.Join(" ", Vertex.vetsToVals(res)));
|
||||
}
|
||||
// 返回顶点遍历序列
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化无向图 */
|
||||
Vertex[] v = Vertex.valsToVets(new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
|
||||
Vertex[][] edges = new Vertex[12][]
|
||||
{
|
||||
new Vertex[2] { v[0], v[1] }, new Vertex[2] { v[0], v[3] }, new Vertex[2] { v[1], v[2] },
|
||||
new Vertex[2] { v[1], v[4] }, new Vertex[2] { v[2], v[5] }, new Vertex[2] { v[3], v[4] },
|
||||
new Vertex[2] { v[3], v[6] }, new Vertex[2] { v[4], v[5] }, new Vertex[2] { v[4], v[7] },
|
||||
new Vertex[2] { v[5], v[8] }, new Vertex[2] { v[6], v[7] }, new Vertex[2] { v[7], v[8] }
|
||||
};
|
||||
|
||||
GraphAdjList graph = new GraphAdjList(edges);
|
||||
Console.WriteLine("\n初始化后,图为");
|
||||
graph.print();
|
||||
|
||||
/* 广度优先遍历 BFS */
|
||||
List<Vertex> res = graphBFS(graph, v[0]);
|
||||
Console.WriteLine("\n广度优先遍历(BFS)顶点序列为");
|
||||
Console.WriteLine(string.Join(" ", Vertex.vetsToVals(res)));
|
||||
}
|
||||
}
|
@ -7,58 +7,57 @@
|
||||
using hello_algo.include;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_graph
|
||||
namespace hello_algo.chapter_graph;
|
||||
|
||||
public class graph_dfs
|
||||
{
|
||||
public class graph_dfs
|
||||
/* 深度优先遍历 DFS 辅助函数 */
|
||||
public void dfs(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet)
|
||||
{
|
||||
/* 深度优先遍历 DFS 辅助函数 */
|
||||
public void dfs(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet)
|
||||
res.Add(vet); // 记录访问顶点
|
||||
visited.Add(vet); // 标记该顶点已被访问
|
||||
// 遍历该顶点的所有邻接顶点
|
||||
foreach (Vertex adjVet in graph.adjList[vet])
|
||||
{
|
||||
res.Add(vet); // 记录访问顶点
|
||||
visited.Add(vet); // 标记该顶点已被访问
|
||||
// 遍历该顶点的所有邻接顶点
|
||||
foreach (Vertex adjVet in graph.adjList[vet])
|
||||
if (visited.Contains(adjVet))
|
||||
{
|
||||
if (visited.Contains(adjVet))
|
||||
{
|
||||
continue; // 跳过已被访问过的顶点
|
||||
}
|
||||
// 递归访问邻接顶点
|
||||
dfs(graph, visited, res, adjVet);
|
||||
continue; // 跳过已被访问过的顶点
|
||||
}
|
||||
}
|
||||
|
||||
/* 深度优先遍历 DFS */
|
||||
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
||||
public List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet)
|
||||
{
|
||||
// 顶点遍历序列
|
||||
List<Vertex> res = new List<Vertex>();
|
||||
// 哈希表,用于记录已被访问过的顶点
|
||||
HashSet<Vertex> visited = new HashSet<Vertex>();
|
||||
dfs(graph, visited, res, startVet);
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化无向图 */
|
||||
Vertex[] v = Vertex.valsToVets(new int[7] { 0, 1, 2, 3, 4, 5, 6 });
|
||||
Vertex[][] edges = new Vertex[6][]
|
||||
{
|
||||
new Vertex[2] { v[0], v[1] }, new Vertex[2] { v[0], v[3] }, new Vertex[2] { v[1], v[2] },
|
||||
new Vertex[2] { v[2], v[5] }, new Vertex[2] { v[4], v[5] }, new Vertex[2] { v[5], v[6] },
|
||||
};
|
||||
|
||||
GraphAdjList graph = new GraphAdjList(edges);
|
||||
Console.WriteLine("\n初始化后,图为");
|
||||
graph.print();
|
||||
|
||||
/* 深度优先遍历 DFS */
|
||||
List<Vertex> res = graphDFS(graph, v[0]);
|
||||
Console.WriteLine("\n深度优先遍历(DFS)顶点序列为");
|
||||
Console.WriteLine(string.Join(" ", Vertex.vetsToVals(res)));
|
||||
// 递归访问邻接顶点
|
||||
dfs(graph, visited, res, adjVet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 深度优先遍历 DFS */
|
||||
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
||||
public List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet)
|
||||
{
|
||||
// 顶点遍历序列
|
||||
List<Vertex> res = new List<Vertex>();
|
||||
// 哈希表,用于记录已被访问过的顶点
|
||||
HashSet<Vertex> visited = new HashSet<Vertex>();
|
||||
dfs(graph, visited, res, startVet);
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化无向图 */
|
||||
Vertex[] v = Vertex.valsToVets(new int[7] { 0, 1, 2, 3, 4, 5, 6 });
|
||||
Vertex[][] edges = new Vertex[6][]
|
||||
{
|
||||
new Vertex[2] { v[0], v[1] }, new Vertex[2] { v[0], v[3] }, new Vertex[2] { v[1], v[2] },
|
||||
new Vertex[2] { v[2], v[5] }, new Vertex[2] { v[4], v[5] }, new Vertex[2] { v[5], v[6] },
|
||||
};
|
||||
|
||||
GraphAdjList graph = new GraphAdjList(edges);
|
||||
Console.WriteLine("\n初始化后,图为");
|
||||
graph.print();
|
||||
|
||||
/* 深度优先遍历 DFS */
|
||||
List<Vertex> res = graphDFS(graph, v[0]);
|
||||
Console.WriteLine("\n深度优先遍历(DFS)顶点序列为");
|
||||
Console.WriteLine(string.Join(" ", Vertex.vetsToVals(res)));
|
||||
}
|
||||
}
|
56
codes/csharp/chapter_sorting/bucked_sort.cs
Normal file
56
codes/csharp/chapter_sorting/bucked_sort.cs
Normal file
@ -0,0 +1,56 @@
|
||||
/**
|
||||
* File: bucked_sort.cs
|
||||
* Created Time: 2023-04-13
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class bucked_sort
|
||||
{
|
||||
/* 桶排序 */
|
||||
public static void bucketSort(float[] nums)
|
||||
{
|
||||
// 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素
|
||||
int k = nums.Length / 2;
|
||||
List<List<float>> buckets = new List<List<float>>();
|
||||
for (int i = 0; i < k; i++)
|
||||
{
|
||||
buckets.Add(new List<float>());
|
||||
}
|
||||
// 1. 将数组元素分配到各个桶中
|
||||
foreach (float num in nums)
|
||||
{
|
||||
// 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
|
||||
int i = (int)num * k;
|
||||
// 将 num 添加进桶 i
|
||||
buckets[i].Add(num);
|
||||
}
|
||||
// 2. 对各个桶执行排序
|
||||
foreach (List<float> bucket in buckets)
|
||||
{
|
||||
// 使用内置排序函数,也可以替换成其他排序算法
|
||||
bucket.Sort();
|
||||
}
|
||||
// 3. 遍历桶合并结果
|
||||
int j = 0;
|
||||
foreach (List<float> bucket in buckets)
|
||||
{
|
||||
foreach (float num in bucket)
|
||||
{
|
||||
nums[j++] = num;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
// 设输入数据为浮点数,范围为 [0, 1)
|
||||
float[] nums = { 0.49f, 0.96f, 0.82f, 0.09f, 0.57f, 0.43f, 0.91f, 0.75f, 0.15f, 0.37f };
|
||||
bucketSort(nums);
|
||||
Console.WriteLine("桶排序完成后 nums = " + string.Join(" ", nums));
|
||||
}
|
||||
}
|
92
codes/csharp/chapter_sorting/counting_sort.cs
Normal file
92
codes/csharp/chapter_sorting/counting_sort.cs
Normal file
@ -0,0 +1,92 @@
|
||||
/**
|
||||
* File: bucked_sort.cs
|
||||
* Created Time: 2023-04-13
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class counting_sort
|
||||
{
|
||||
/* 计数排序 */
|
||||
// 简单实现,无法用于排序对象
|
||||
public static void countingSortNaive(int[] nums)
|
||||
{
|
||||
// 1. 统计数组最大元素 m
|
||||
int m = 0;
|
||||
foreach (int num in nums)
|
||||
{
|
||||
m = Math.Max(m, num);
|
||||
}
|
||||
// 2. 统计各数字的出现次数
|
||||
// counter[num] 代表 num 的出现次数
|
||||
int[] counter = new int[m + 1];
|
||||
foreach (int num in nums)
|
||||
{
|
||||
counter[num]++;
|
||||
}
|
||||
// 3. 遍历 counter ,将各元素填入原数组 nums
|
||||
int i = 0;
|
||||
for (int num = 0; num < m + 1; num++)
|
||||
{
|
||||
for (int j = 0; j < counter[num]; j++, i++)
|
||||
{
|
||||
nums[i] = num;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 计数排序 */
|
||||
// 完整实现,可排序对象,并且是稳定排序
|
||||
static void countingSort(int[] nums)
|
||||
{
|
||||
// 1. 统计数组最大元素 m
|
||||
int m = 0;
|
||||
foreach (int num in nums)
|
||||
{
|
||||
m = Math.Max(m, num);
|
||||
}
|
||||
// 2. 统计各数字的出现次数
|
||||
// counter[num] 代表 num 的出现次数
|
||||
int[] counter = new int[m + 1];
|
||||
foreach (int num in nums)
|
||||
{
|
||||
counter[num]++;
|
||||
}
|
||||
// 3. 求 counter 的前缀和,将“出现次数”转换为“尾索引”
|
||||
// 即 counter[num]-1 是 num 在 res 中最后一次出现的索引
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
counter[i + 1] += counter[i];
|
||||
}
|
||||
// 4. 倒序遍历 nums ,将各元素填入结果数组 res
|
||||
// 初始化数组 res 用于记录结果
|
||||
int n = nums.Length;
|
||||
int[] res = new int[n];
|
||||
for (int i = n - 1; i >= 0; i--)
|
||||
{
|
||||
int num = nums[i];
|
||||
res[counter[num] - 1] = num; // 将 num 放置到对应索引处
|
||||
counter[num]--; // 令前缀和自减 1 ,得到下次放置 num 的索引
|
||||
}
|
||||
// 使用结果数组 res 覆盖原数组 nums
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
nums[i] = res[i];
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
int[] nums = { 1, 0, 1, 2, 0, 4, 0, 2, 2, 4 };
|
||||
countingSortNaive(nums);
|
||||
Console.WriteLine("计数排序(无法排序对象)完成后 nums = " + string.Join(" ", nums));
|
||||
|
||||
int[] nums1 = { 1, 0, 1, 2, 0, 4, 0, 2, 2, 4 };
|
||||
countingSort(nums1);
|
||||
Console.WriteLine("计数排序完成后 nums1 = " + string.Join(" ", nums));
|
||||
}
|
||||
}
|
82
codes/csharp/chapter_sorting/radix_sort.cs
Normal file
82
codes/csharp/chapter_sorting/radix_sort.cs
Normal file
@ -0,0 +1,82 @@
|
||||
/**
|
||||
* File: bucked_sort.cs
|
||||
* Created Time: 2023-04-13
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class radix_sort
|
||||
{
|
||||
/* 获取元素 num 的第 k 位,其中 exp = 10^(k-1) */
|
||||
static int digit(int num, int exp)
|
||||
{
|
||||
// 传入 exp 而非 k 可以避免在此重复执行昂贵的次方计算
|
||||
return (num / exp) % 10;
|
||||
}
|
||||
|
||||
/* 计数排序(根据 nums 第 k 位排序) */
|
||||
static void countingSortDigit(int[] nums, int exp)
|
||||
{
|
||||
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶
|
||||
int[] counter = new int[10];
|
||||
int n = nums.Length;
|
||||
// 统计 0~9 各数字的出现次数
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
int d = digit(nums[i], exp); // 获取 nums[i] 第 k 位,记为 d
|
||||
counter[d]++; // 统计数字 d 的出现次数
|
||||
}
|
||||
// 求前缀和,将“出现个数”转换为“数组索引”
|
||||
for (int i = 1; i < 10; i++)
|
||||
{
|
||||
counter[i] += counter[i - 1];
|
||||
}
|
||||
// 倒序遍历,根据桶内统计结果,将各元素填入 res
|
||||
int[] res = new int[n];
|
||||
for (int i = n - 1; i >= 0; i--)
|
||||
{
|
||||
int d = digit(nums[i], exp);
|
||||
int j = counter[d] - 1; // 获取 d 在数组中的索引 j
|
||||
res[j] = nums[i]; // 将当前元素填入索引 j
|
||||
counter[d]--; // 将 d 的数量减 1
|
||||
}
|
||||
// 使用结果覆盖原数组 nums
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
nums[i] = res[i];
|
||||
}
|
||||
}
|
||||
|
||||
/* 基数排序 */
|
||||
static void radixSort(int[] nums)
|
||||
{
|
||||
// 获取数组的最大元素,用于判断最大位数
|
||||
int m = int.MinValue;
|
||||
foreach (int num in nums)
|
||||
{
|
||||
if (num > m) m = num;
|
||||
}
|
||||
// 按照从低位到高位的顺序遍历
|
||||
for (int exp = 1; exp <= m; exp *= 10)
|
||||
{
|
||||
// 对数组元素的第 k 位执行计数排序
|
||||
// k = 1 -> exp = 1
|
||||
// k = 2 -> exp = 10
|
||||
// 即 exp = 10^(k-1)
|
||||
countingSortDigit(nums, exp);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
// 基数排序
|
||||
int[] nums = { 10546151, 35663510, 42865989, 34862445, 81883077,
|
||||
88906420, 72429244, 30524779, 82060337, 63832996 };
|
||||
radixSort(nums);
|
||||
Console.WriteLine("基数排序完成后 nums = " + string.Join(" ", nums));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user