mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 21:24:53 +08:00
fix(csharp): Modify method name to PascalCase, simplify new expression (#840)
* Modify method name to PascalCase(array and linked list) * Modify method name to PascalCase(backtracking) * Modify method name to PascalCase(computational complexity) * Modify method name to PascalCase(divide and conquer) * Modify method name to PascalCase(dynamic programming) * Modify method name to PascalCase(graph) * Modify method name to PascalCase(greedy) * Modify method name to PascalCase(hashing) * Modify method name to PascalCase(heap) * Modify method name to PascalCase(searching) * Modify method name to PascalCase(sorting) * Modify method name to PascalCase(stack and queue) * Modify method name to PascalCase(tree) * local check
This commit is contained in:
@ -8,23 +8,21 @@ namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class bubble_sort {
|
||||
/* 冒泡排序 */
|
||||
static void bubbleSort(int[] nums) {
|
||||
static void BubbleSort(int[] nums) {
|
||||
// 外循环:未排序区间为 [0, i]
|
||||
for (int i = nums.Length - 1; i > 0; i--) {
|
||||
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 冒泡排序(标志优化)*/
|
||||
static void bubbleSortWithFlag(int[] nums) {
|
||||
static void BubbleSortWithFlag(int[] nums) {
|
||||
// 外循环:未排序区间为 [0, i]
|
||||
for (int i = nums.Length - 1; i > 0; i--) {
|
||||
bool flag = false; // 初始化标志位
|
||||
@ -32,9 +30,7 @@ public class bubble_sort {
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);
|
||||
flag = true; // 记录交换元素
|
||||
}
|
||||
}
|
||||
@ -45,11 +41,11 @@ public class bubble_sort {
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = { 4, 1, 3, 1, 5, 2 };
|
||||
bubbleSort(nums);
|
||||
BubbleSort(nums);
|
||||
Console.WriteLine("冒泡排序完成后 nums = " + string.Join(",", nums));
|
||||
|
||||
int[] nums1 = { 4, 1, 3, 1, 5, 2 };
|
||||
bubbleSortWithFlag(nums1);
|
||||
BubbleSortWithFlag(nums1);
|
||||
Console.WriteLine("冒泡排序完成后 nums1 = " + string.Join(",", nums1));
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,10 +8,10 @@ namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class bucket_sort {
|
||||
/* 桶排序 */
|
||||
public static void bucketSort(float[] nums) {
|
||||
public static void BucketSort(float[] nums) {
|
||||
// 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素
|
||||
int k = nums.Length / 2;
|
||||
List<List<float>> buckets = new List<List<float>>();
|
||||
List<List<float>> buckets = new();
|
||||
for (int i = 0; i < k; i++) {
|
||||
buckets.Add(new List<float>());
|
||||
}
|
||||
@ -40,7 +40,7 @@ public class bucket_sort {
|
||||
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);
|
||||
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) {
|
||||
public static 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) {
|
||||
static void CountingSort(int[] nums) {
|
||||
// 1. 统计数组最大元素 m
|
||||
int m = 0;
|
||||
foreach (int num in nums) {
|
||||
@ -67,11 +67,11 @@ public class counting_sort {
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = { 1, 0, 1, 2, 0, 4, 0, 2, 2, 4 };
|
||||
countingSortNaive(nums);
|
||||
CountingSortNaive(nums);
|
||||
Console.WriteLine("计数排序(无法排序对象)完成后 nums = " + string.Join(" ", nums));
|
||||
|
||||
int[] nums1 = { 1, 0, 1, 2, 0, 4, 0, 2, 2, 4 };
|
||||
countingSort(nums1);
|
||||
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) {
|
||||
public static void SiftDown(int[] nums, int n, int i) {
|
||||
while (true) {
|
||||
// 判断节点 i, l, r 中值最大的节点,记为 ma
|
||||
int l = 2 * i + 1;
|
||||
@ -29,24 +29,24 @@ public class heap_sort {
|
||||
}
|
||||
|
||||
/* 堆排序 */
|
||||
public static void heapSort(int[] nums) {
|
||||
public static void HeapSort(int[] nums) {
|
||||
// 建堆操作:堆化除叶节点以外的其他所有节点
|
||||
for (int i = nums.Length / 2 - 1; i >= 0; i--) {
|
||||
siftDown(nums, nums.Length, i);
|
||||
SiftDown(nums, nums.Length, i);
|
||||
}
|
||||
// 从堆中提取最大元素,循环 n-1 轮
|
||||
for (int i = nums.Length - 1; i > 0; i--) {
|
||||
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
||||
(nums[i], nums[0]) = (nums[0], nums[i]);
|
||||
// 以根节点为起点,从顶至底进行堆化
|
||||
siftDown(nums, i, 0);
|
||||
SiftDown(nums, i, 0);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = { 4, 1, 3, 1, 5, 2 };
|
||||
heapSort(nums);
|
||||
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) {
|
||||
static void InsertionSort(int[] nums) {
|
||||
// 外循环:已排序元素数量为 1, 2, ..., n
|
||||
for (int i = 1; i < nums.Length; i++) {
|
||||
int bas = nums[i], j = i - 1;
|
||||
@ -24,7 +24,7 @@ public class insertion_sort {
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = { 4, 1, 3, 1, 5, 2 };
|
||||
insertionSort(nums);
|
||||
InsertionSort(nums);
|
||||
Console.WriteLine("插入排序完成后 nums = " + string.Join(",", nums));
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ public class merge_sort {
|
||||
/* 合并左子数组和右子数组 */
|
||||
// 左子数组区间 [left, mid]
|
||||
// 右子数组区间 [mid + 1, right]
|
||||
static void merge(int[] nums, int left, int mid, int right) {
|
||||
static void Merge(int[] nums, int left, int mid, int right) {
|
||||
// 初始化辅助数组
|
||||
int[] tmp = nums[left..(right + 1)];
|
||||
// 左子数组的起始索引和结束索引
|
||||
@ -34,22 +34,22 @@ public class merge_sort {
|
||||
}
|
||||
|
||||
/* 归并排序 */
|
||||
static void mergeSort(int[] nums, int left, int right) {
|
||||
static void MergeSort(int[] nums, int left, int right) {
|
||||
// 终止条件
|
||||
if (left >= right) return; // 当子数组长度为 1 时终止递归
|
||||
// 划分阶段
|
||||
int mid = (left + right) / 2; // 计算中点
|
||||
mergeSort(nums, left, mid); // 递归左子数组
|
||||
mergeSort(nums, mid + 1, right); // 递归右子数组
|
||||
MergeSort(nums, left, mid); // 递归左子数组
|
||||
MergeSort(nums, mid + 1, right); // 递归右子数组
|
||||
// 合并阶段
|
||||
merge(nums, left, mid, right);
|
||||
Merge(nums, left, mid, right);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 归并排序 */
|
||||
int[] nums = { 7, 3, 2, 6, 0, 1, 5, 4 };
|
||||
mergeSort(nums, 0, nums.Length - 1);
|
||||
MergeSort(nums, 0, nums.Length - 1);
|
||||
Console.WriteLine("归并排序完成后 nums = " + string.Join(",", nums));
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,16 +6,14 @@
|
||||
|
||||
namespace hello_algo.chapter_sorting;
|
||||
|
||||
class QuickSort {
|
||||
class quickSort {
|
||||
/* 元素交换 */
|
||||
static void swap(int[] nums, int i, int j) {
|
||||
int tmp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = tmp;
|
||||
static void Swap(int[] nums, int i, int j) {
|
||||
(nums[j], nums[i]) = (nums[i], nums[j]);
|
||||
}
|
||||
|
||||
/* 哨兵划分 */
|
||||
static int partition(int[] nums, int left, int right) {
|
||||
static int Partition(int[] nums, int left, int right) {
|
||||
// 以 nums[left] 作为基准数
|
||||
int i = left, j = right;
|
||||
while (i < j) {
|
||||
@ -23,36 +21,34 @@ class QuickSort {
|
||||
j--; // 从右向左找首个小于基准数的元素
|
||||
while (i < j && nums[i] <= nums[left])
|
||||
i++; // 从左向右找首个大于基准数的元素
|
||||
swap(nums, i, j); // 交换这两个元素
|
||||
Swap(nums, i, j); // 交换这两个元素
|
||||
}
|
||||
swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
Swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
return i; // 返回基准数的索引
|
||||
}
|
||||
|
||||
/* 快速排序 */
|
||||
public static void quickSort(int[] nums, int left, int right) {
|
||||
public static void QuickSort(int[] nums, int left, int right) {
|
||||
// 子数组长度为 1 时终止递归
|
||||
if (left >= right)
|
||||
return;
|
||||
// 哨兵划分
|
||||
int pivot = partition(nums, left, right);
|
||||
int pivot = Partition(nums, left, right);
|
||||
// 递归左子数组、右子数组
|
||||
quickSort(nums, left, pivot - 1);
|
||||
quickSort(nums, pivot + 1, right);
|
||||
QuickSort(nums, left, pivot - 1);
|
||||
QuickSort(nums, pivot + 1, right);
|
||||
}
|
||||
}
|
||||
|
||||
/* 快速排序类(中位基准数优化) */
|
||||
class QuickSortMedian {
|
||||
/* 元素交换 */
|
||||
static void swap(int[] nums, int i, int j) {
|
||||
int tmp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = tmp;
|
||||
static void Swap(int[] nums, int i, int j) {
|
||||
(nums[j], nums[i]) = (nums[i], nums[j]);
|
||||
}
|
||||
|
||||
/* 选取三个元素的中位数 */
|
||||
static int medianThree(int[] nums, int left, int mid, int right) {
|
||||
static int MedianThree(int[] nums, int left, int mid, int right) {
|
||||
// 此处使用异或运算来简化代码
|
||||
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
||||
if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))
|
||||
@ -64,11 +60,11 @@ class QuickSortMedian {
|
||||
}
|
||||
|
||||
/* 哨兵划分(三数取中值) */
|
||||
static int partition(int[] nums, int left, int right) {
|
||||
static int Partition(int[] nums, int left, int right) {
|
||||
// 选取三个候选元素的中位数
|
||||
int med = medianThree(nums, left, (left + right) / 2, right);
|
||||
int med = MedianThree(nums, left, (left + right) / 2, right);
|
||||
// 将中位数交换至数组最左端
|
||||
swap(nums, left, med);
|
||||
Swap(nums, left, med);
|
||||
// 以 nums[left] 作为基准数
|
||||
int i = left, j = right;
|
||||
while (i < j) {
|
||||
@ -76,36 +72,34 @@ class QuickSortMedian {
|
||||
j--; // 从右向左找首个小于基准数的元素
|
||||
while (i < j && nums[i] <= nums[left])
|
||||
i++; // 从左向右找首个大于基准数的元素
|
||||
swap(nums, i, j); // 交换这两个元素
|
||||
Swap(nums, i, j); // 交换这两个元素
|
||||
}
|
||||
swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
Swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
return i; // 返回基准数的索引
|
||||
}
|
||||
|
||||
/* 快速排序 */
|
||||
public static void quickSort(int[] nums, int left, int right) {
|
||||
public static void QuickSort(int[] nums, int left, int right) {
|
||||
// 子数组长度为 1 时终止递归
|
||||
if (left >= right)
|
||||
return;
|
||||
// 哨兵划分
|
||||
int pivot = partition(nums, left, right);
|
||||
int pivot = Partition(nums, left, right);
|
||||
// 递归左子数组、右子数组
|
||||
quickSort(nums, left, pivot - 1);
|
||||
quickSort(nums, pivot + 1, right);
|
||||
QuickSort(nums, left, pivot - 1);
|
||||
QuickSort(nums, pivot + 1, right);
|
||||
}
|
||||
}
|
||||
|
||||
/* 快速排序类(尾递归优化) */
|
||||
class QuickSortTailCall {
|
||||
/* 元素交换 */
|
||||
static void swap(int[] nums, int i, int j) {
|
||||
int tmp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = tmp;
|
||||
static void Swap(int[] nums, int i, int j) {
|
||||
(nums[j], nums[i]) = (nums[i], nums[j]);
|
||||
}
|
||||
|
||||
/* 哨兵划分 */
|
||||
static int partition(int[] nums, int left, int right) {
|
||||
static int Partition(int[] nums, int left, int right) {
|
||||
// 以 nums[left] 作为基准数
|
||||
int i = left, j = right;
|
||||
while (i < j) {
|
||||
@ -113,24 +107,24 @@ class QuickSortTailCall {
|
||||
j--; // 从右向左找首个小于基准数的元素
|
||||
while (i < j && nums[i] <= nums[left])
|
||||
i++; // 从左向右找首个大于基准数的元素
|
||||
swap(nums, i, j); // 交换这两个元素
|
||||
Swap(nums, i, j); // 交换这两个元素
|
||||
}
|
||||
swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
Swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
return i; // 返回基准数的索引
|
||||
}
|
||||
|
||||
/* 快速排序(尾递归优化) */
|
||||
public static void quickSort(int[] nums, int left, int right) {
|
||||
public static void QuickSort(int[] nums, int left, int right) {
|
||||
// 子数组长度为 1 时终止
|
||||
while (left < right) {
|
||||
// 哨兵划分操作
|
||||
int pivot = partition(nums, left, right);
|
||||
int pivot = Partition(nums, left, right);
|
||||
// 对两个子数组中较短的那个执行快排
|
||||
if (pivot - left < right - pivot) {
|
||||
quickSort(nums, left, pivot - 1); // 递归排序左子数组
|
||||
QuickSort(nums, left, pivot - 1); // 递归排序左子数组
|
||||
left = pivot + 1; // 剩余未排序区间为 [pivot + 1, right]
|
||||
} else {
|
||||
quickSort(nums, pivot + 1, right); // 递归排序右子数组
|
||||
QuickSort(nums, pivot + 1, right); // 递归排序右子数组
|
||||
right = pivot - 1; // 剩余未排序区间为 [left, pivot - 1]
|
||||
}
|
||||
}
|
||||
@ -142,17 +136,17 @@ public class quick_sort {
|
||||
public void Test() {
|
||||
/* 快速排序 */
|
||||
int[] nums = { 2, 4, 1, 0, 3, 5 };
|
||||
QuickSort.quickSort(nums, 0, nums.Length - 1);
|
||||
quickSort.QuickSort(nums, 0, nums.Length - 1);
|
||||
Console.WriteLine("快速排序完成后 nums = " + string.Join(",", nums));
|
||||
|
||||
/* 快速排序(中位基准数优化) */
|
||||
int[] nums1 = { 2, 4, 1, 0, 3, 5 };
|
||||
QuickSortMedian.quickSort(nums1, 0, nums1.Length - 1);
|
||||
QuickSortMedian.QuickSort(nums1, 0, nums1.Length - 1);
|
||||
Console.WriteLine("快速排序(中位基准数优化)完成后 nums1 = " + string.Join(",", nums1));
|
||||
|
||||
/* 快速排序(尾递归优化) */
|
||||
int[] nums2 = { 2, 4, 1, 0, 3, 5 };
|
||||
QuickSortTailCall.quickSort(nums2, 0, nums2.Length - 1);
|
||||
QuickSortTailCall.QuickSort(nums2, 0, nums2.Length - 1);
|
||||
Console.WriteLine("快速排序(尾递归优化)完成后 nums2 = " + string.Join(",", nums2));
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,19 +8,19 @@ namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class radix_sort {
|
||||
/* 获取元素 num 的第 k 位,其中 exp = 10^(k-1) */
|
||||
static int digit(int num, int exp) {
|
||||
static int Digit(int num, int exp) {
|
||||
// 传入 exp 而非 k 可以避免在此重复执行昂贵的次方计算
|
||||
return (num / exp) % 10;
|
||||
}
|
||||
|
||||
/* 计数排序(根据 nums 第 k 位排序) */
|
||||
static void countingSortDigit(int[] nums, int exp) {
|
||||
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
|
||||
int d = Digit(nums[i], exp); // 获取 nums[i] 第 k 位,记为 d
|
||||
counter[d]++; // 统计数字 d 的出现次数
|
||||
}
|
||||
// 求前缀和,将“出现个数”转换为“数组索引”
|
||||
@ -30,7 +30,7 @@ public class radix_sort {
|
||||
// 倒序遍历,根据桶内统计结果,将各元素填入 res
|
||||
int[] res = new int[n];
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
int d = digit(nums[i], exp);
|
||||
int d = Digit(nums[i], exp);
|
||||
int j = counter[d] - 1; // 获取 d 在数组中的索引 j
|
||||
res[j] = nums[i]; // 将当前元素填入索引 j
|
||||
counter[d]--; // 将 d 的数量减 1
|
||||
@ -42,7 +42,7 @@ public class radix_sort {
|
||||
}
|
||||
|
||||
/* 基数排序 */
|
||||
static void radixSort(int[] nums) {
|
||||
static void RadixSort(int[] nums) {
|
||||
// 获取数组的最大元素,用于判断最大位数
|
||||
int m = int.MinValue;
|
||||
foreach (int num in nums) {
|
||||
@ -54,7 +54,7 @@ public class radix_sort {
|
||||
// k = 1 -> exp = 1
|
||||
// k = 2 -> exp = 10
|
||||
// 即 exp = 10^(k-1)
|
||||
countingSortDigit(nums, exp);
|
||||
CountingSortDigit(nums, exp);
|
||||
}
|
||||
}
|
||||
|
||||
@ -63,7 +63,7 @@ public class radix_sort {
|
||||
// 基数排序
|
||||
int[] nums = { 10546151, 35663510, 42865989, 34862445, 81883077,
|
||||
88906420, 72429244, 30524779, 82060337, 63832996 };
|
||||
radixSort(nums);
|
||||
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) {
|
||||
public static void SelectionSort(int[] nums) {
|
||||
int n = nums.Length;
|
||||
// 外循环:未排序区间为 [i, n-1]
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
@ -26,7 +26,7 @@ public class selection_sort {
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = { 4, 1, 3, 1, 5, 2 };
|
||||
selectionSort(nums);
|
||||
SelectionSort(nums);
|
||||
Console.WriteLine("选择排序完成后 nums = " + string.Join(" ", nums));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user