mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 11:41:22 +08:00
feat(csharp) .NET 8.0 code migration (#966)
* .net 8.0 migration * update docs * revert change * revert change and update appendix docs * remove static * Update binary_search_insertion.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class bubble_sort {
|
||||
/* 冒泡排序 */
|
||||
static void BubbleSort(int[] nums) {
|
||||
void BubbleSort(int[] nums) {
|
||||
// 外循环:未排序区间为 [0, i]
|
||||
for (int i = nums.Length - 1; i > 0; i--) {
|
||||
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
||||
@@ -22,7 +22,7 @@ public class bubble_sort {
|
||||
}
|
||||
|
||||
/* 冒泡排序(标志优化)*/
|
||||
static void BubbleSortWithFlag(int[] nums) {
|
||||
void BubbleSortWithFlag(int[] nums) {
|
||||
// 外循环:未排序区间为 [0, i]
|
||||
for (int i = nums.Length - 1; i > 0; i--) {
|
||||
bool flag = false; // 初始化标志位
|
||||
@@ -40,11 +40,11 @@ public class bubble_sort {
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = { 4, 1, 3, 1, 5, 2 };
|
||||
int[] nums = [4, 1, 3, 1, 5, 2];
|
||||
BubbleSort(nums);
|
||||
Console.WriteLine("冒泡排序完成后 nums = " + string.Join(",", nums));
|
||||
|
||||
int[] nums1 = { 4, 1, 3, 1, 5, 2 };
|
||||
int[] nums1 = [4, 1, 3, 1, 5, 2];
|
||||
BubbleSortWithFlag(nums1);
|
||||
Console.WriteLine("冒泡排序完成后 nums1 = " + string.Join(",", nums1));
|
||||
}
|
||||
|
||||
@@ -8,12 +8,12 @@ namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class bucket_sort {
|
||||
/* 桶排序 */
|
||||
public static void BucketSort(float[] nums) {
|
||||
void BucketSort(float[] nums) {
|
||||
// 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素
|
||||
int k = nums.Length / 2;
|
||||
List<List<float>> buckets = new();
|
||||
List<List<float>> buckets = [];
|
||||
for (int i = 0; i < k; i++) {
|
||||
buckets.Add(new List<float>());
|
||||
buckets.Add([]);
|
||||
}
|
||||
// 1. 将数组元素分配到各个桶中
|
||||
foreach (float num in nums) {
|
||||
@@ -39,7 +39,7 @@ public class bucket_sort {
|
||||
[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 };
|
||||
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));
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace hello_algo.chapter_sorting;
|
||||
public class counting_sort {
|
||||
/* 计数排序 */
|
||||
// 简单实现,无法用于排序对象
|
||||
public static void CountingSortNaive(int[] nums) {
|
||||
void CountingSortNaive(int[] nums) {
|
||||
// 1. 统计数组最大元素 m
|
||||
int m = 0;
|
||||
foreach (int num in nums) {
|
||||
@@ -32,7 +32,7 @@ public class counting_sort {
|
||||
|
||||
/* 计数排序 */
|
||||
// 完整实现,可排序对象,并且是稳定排序
|
||||
static void CountingSort(int[] nums) {
|
||||
void CountingSort(int[] nums) {
|
||||
// 1. 统计数组最大元素 m
|
||||
int m = 0;
|
||||
foreach (int num in nums) {
|
||||
@@ -66,11 +66,11 @@ public class counting_sort {
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = { 1, 0, 1, 2, 0, 4, 0, 2, 2, 4 };
|
||||
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 };
|
||||
int[] nums1 = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4];
|
||||
CountingSort(nums1);
|
||||
Console.WriteLine("计数排序完成后 nums1 = " + string.Join(" ", nums));
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class heap_sort {
|
||||
/* 堆的长度为 n ,从节点 i 开始,从顶至底堆化 */
|
||||
public static void SiftDown(int[] nums, int n, int i) {
|
||||
void SiftDown(int[] nums, int n, int i) {
|
||||
while (true) {
|
||||
// 判断节点 i, l, r 中值最大的节点,记为 ma
|
||||
int l = 2 * i + 1;
|
||||
@@ -29,7 +29,7 @@ public class heap_sort {
|
||||
}
|
||||
|
||||
/* 堆排序 */
|
||||
public static void HeapSort(int[] nums) {
|
||||
void HeapSort(int[] nums) {
|
||||
// 建堆操作:堆化除叶节点以外的其他所有节点
|
||||
for (int i = nums.Length / 2 - 1; i >= 0; i--) {
|
||||
SiftDown(nums, nums.Length, i);
|
||||
@@ -45,7 +45,7 @@ public class heap_sort {
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = { 4, 1, 3, 1, 5, 2 };
|
||||
int[] nums = [4, 1, 3, 1, 5, 2];
|
||||
HeapSort(nums);
|
||||
Console.WriteLine("堆排序完成后 nums = " + string.Join(" ", nums));
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class insertion_sort {
|
||||
/* 插入排序 */
|
||||
static void InsertionSort(int[] nums) {
|
||||
void InsertionSort(int[] nums) {
|
||||
// 外循环:已排序元素数量为 1, 2, ..., n
|
||||
for (int i = 1; i < nums.Length; i++) {
|
||||
int bas = nums[i], j = i - 1;
|
||||
@@ -23,7 +23,7 @@ public class insertion_sort {
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = { 4, 1, 3, 1, 5, 2 };
|
||||
int[] nums = [4, 1, 3, 1, 5, 2];
|
||||
InsertionSort(nums);
|
||||
Console.WriteLine("插入排序完成后 nums = " + string.Join(",", nums));
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class merge_sort {
|
||||
/* 合并左子数组和右子数组 */
|
||||
static void Merge(int[] nums, int left, int mid, int right) {
|
||||
void Merge(int[] nums, int left, int mid, int right) {
|
||||
// 左子数组区间 [left, mid], 右子数组区间 [mid+1, right]
|
||||
// 创建一个临时数组 tmp ,用于存放合并后的结果
|
||||
int[] tmp = new int[right - left + 1];
|
||||
@@ -35,7 +35,7 @@ public class merge_sort {
|
||||
}
|
||||
|
||||
/* 归并排序 */
|
||||
static void MergeSort(int[] nums, int left, int right) {
|
||||
void MergeSort(int[] nums, int left, int right) {
|
||||
// 终止条件
|
||||
if (left >= right) return; // 当子数组长度为 1 时终止递归
|
||||
// 划分阶段
|
||||
@@ -49,7 +49,7 @@ public class merge_sort {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 归并排序 */
|
||||
int[] nums = { 7, 3, 2, 6, 0, 1, 5, 4 };
|
||||
int[] nums = [7, 3, 2, 6, 0, 1, 5, 4];
|
||||
MergeSort(nums, 0, nums.Length - 1);
|
||||
Console.WriteLine("归并排序完成后 nums = " + string.Join(",", nums));
|
||||
}
|
||||
|
||||
@@ -135,17 +135,17 @@ public class quick_sort {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 快速排序 */
|
||||
int[] nums = { 2, 4, 1, 0, 3, 5 };
|
||||
int[] nums = [2, 4, 1, 0, 3, 5];
|
||||
quickSort.QuickSort(nums, 0, nums.Length - 1);
|
||||
Console.WriteLine("快速排序完成后 nums = " + string.Join(",", nums));
|
||||
|
||||
/* 快速排序(中位基准数优化) */
|
||||
int[] nums1 = { 2, 4, 1, 0, 3, 5 };
|
||||
int[] nums1 = [2, 4, 1, 0, 3, 5];
|
||||
QuickSortMedian.QuickSort(nums1, 0, nums1.Length - 1);
|
||||
Console.WriteLine("快速排序(中位基准数优化)完成后 nums1 = " + string.Join(",", nums1));
|
||||
|
||||
/* 快速排序(尾递归优化) */
|
||||
int[] nums2 = { 2, 4, 1, 0, 3, 5 };
|
||||
int[] nums2 = [2, 4, 1, 0, 3, 5];
|
||||
QuickSortTailCall.QuickSort(nums2, 0, nums2.Length - 1);
|
||||
Console.WriteLine("快速排序(尾递归优化)完成后 nums2 = " + string.Join(",", nums2));
|
||||
}
|
||||
|
||||
@@ -8,13 +8,13 @@ namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class radix_sort {
|
||||
/* 获取元素 num 的第 k 位,其中 exp = 10^(k-1) */
|
||||
static int Digit(int num, int exp) {
|
||||
int Digit(int num, int exp) {
|
||||
// 传入 exp 而非 k 可以避免在此重复执行昂贵的次方计算
|
||||
return (num / exp) % 10;
|
||||
}
|
||||
|
||||
/* 计数排序(根据 nums 第 k 位排序) */
|
||||
static void CountingSortDigit(int[] nums, int exp) {
|
||||
void CountingSortDigit(int[] nums, int exp) {
|
||||
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶
|
||||
int[] counter = new int[10];
|
||||
int n = nums.Length;
|
||||
@@ -42,7 +42,7 @@ public class radix_sort {
|
||||
}
|
||||
|
||||
/* 基数排序 */
|
||||
static void RadixSort(int[] nums) {
|
||||
void RadixSort(int[] nums) {
|
||||
// 获取数组的最大元素,用于判断最大位数
|
||||
int m = int.MinValue;
|
||||
foreach (int num in nums) {
|
||||
@@ -61,8 +61,8 @@ public class radix_sort {
|
||||
[Test]
|
||||
public void Test() {
|
||||
// 基数排序
|
||||
int[] nums = { 10546151, 35663510, 42865989, 34862445, 81883077,
|
||||
88906420, 72429244, 30524779, 82060337, 63832996 };
|
||||
int[] nums = [ 10546151, 35663510, 42865989, 34862445, 81883077,
|
||||
88906420, 72429244, 30524779, 82060337, 63832996 ];
|
||||
RadixSort(nums);
|
||||
Console.WriteLine("基数排序完成后 nums = " + string.Join(" ", nums));
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class selection_sort {
|
||||
/* 选择排序 */
|
||||
public static void SelectionSort(int[] nums) {
|
||||
void SelectionSort(int[] nums) {
|
||||
int n = nums.Length;
|
||||
// 外循环:未排序区间为 [i, n-1]
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
@@ -25,7 +25,7 @@ public class selection_sort {
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = { 4, 1, 3, 1, 5, 2 };
|
||||
int[] nums = [4, 1, 3, 1, 5, 2];
|
||||
SelectionSort(nums);
|
||||
Console.WriteLine("选择排序完成后 nums = " + string.Join(" ", nums));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user