mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 10:39:54 +08:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
51
en/codes/csharp/chapter_sorting/bubble_sort.cs
Normal file
51
en/codes/csharp/chapter_sorting/bubble_sort.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* File: bubble_sort.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class bubble_sort {
|
||||
/* Bubble sort */
|
||||
void BubbleSort(int[] nums) {
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
for (int i = nums.Length - 1; i > 0; i--) {
|
||||
// Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// Swap nums[j] and nums[j + 1]
|
||||
(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Bubble sort (flag optimization) */
|
||||
void BubbleSortWithFlag(int[] nums) {
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
for (int i = nums.Length - 1; i > 0; i--) {
|
||||
bool flag = false; // Initialize flag
|
||||
// Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// Swap nums[j] and nums[j + 1]
|
||||
(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);
|
||||
flag = true; // Record element swap
|
||||
}
|
||||
}
|
||||
if (!flag) break; // No elements were swapped in this round of "bubbling", exit directly
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = [4, 1, 3, 1, 5, 2];
|
||||
BubbleSort(nums);
|
||||
Console.WriteLine("After bubble sort, nums = " + string.Join(",", nums));
|
||||
|
||||
int[] nums1 = [4, 1, 3, 1, 5, 2];
|
||||
BubbleSortWithFlag(nums1);
|
||||
Console.WriteLine("After bubble sort completes, nums1 = " + string.Join(",", nums1));
|
||||
}
|
||||
}
|
||||
46
en/codes/csharp/chapter_sorting/bucket_sort.cs
Normal file
46
en/codes/csharp/chapter_sorting/bucket_sort.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* File: bucket_sort.cs
|
||||
* Created Time: 2023-04-13
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class bucket_sort {
|
||||
/* Bucket sort */
|
||||
void BucketSort(float[] nums) {
|
||||
// Initialize k = n/2 buckets, expected to allocate 2 elements per bucket
|
||||
int k = nums.Length / 2;
|
||||
List<List<float>> buckets = [];
|
||||
for (int i = 0; i < k; i++) {
|
||||
buckets.Add([]);
|
||||
}
|
||||
// 1. Distribute array elements into various buckets
|
||||
foreach (float num in nums) {
|
||||
// Input data range is [0, 1), use num * k to map to index range [0, k-1]
|
||||
int i = (int)(num * k);
|
||||
// Add num to bucket i
|
||||
buckets[i].Add(num);
|
||||
}
|
||||
// 2. Sort each bucket
|
||||
foreach (List<float> bucket in buckets) {
|
||||
// Use built-in sorting function, can also replace with other sorting algorithms
|
||||
bucket.Sort();
|
||||
}
|
||||
// 3. Traverse buckets to merge results
|
||||
int j = 0;
|
||||
foreach (List<float> bucket in buckets) {
|
||||
foreach (float num in bucket) {
|
||||
nums[j++] = num;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
// Assume input data is floating point, interval [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("After bucket sort completes, nums = " + string.Join(" ", nums));
|
||||
}
|
||||
}
|
||||
77
en/codes/csharp/chapter_sorting/counting_sort.cs
Normal file
77
en/codes/csharp/chapter_sorting/counting_sort.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* File: counting_sort.cs
|
||||
* Created Time: 2023-04-13
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class counting_sort {
|
||||
/* Counting sort */
|
||||
// Simple implementation, cannot be used for sorting objects
|
||||
void CountingSortNaive(int[] nums) {
|
||||
// 1. Count the maximum element m in the array
|
||||
int m = 0;
|
||||
foreach (int num in nums) {
|
||||
m = Math.Max(m, num);
|
||||
}
|
||||
// 2. Count the occurrence of each number
|
||||
// counter[num] represents the occurrence of num
|
||||
int[] counter = new int[m + 1];
|
||||
foreach (int num in nums) {
|
||||
counter[num]++;
|
||||
}
|
||||
// 3. Traverse counter, filling each element back into the original array nums
|
||||
int i = 0;
|
||||
for (int num = 0; num < m + 1; num++) {
|
||||
for (int j = 0; j < counter[num]; j++, i++) {
|
||||
nums[i] = num;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Counting sort */
|
||||
// Complete implementation, can sort objects and is a stable sort
|
||||
void CountingSort(int[] nums) {
|
||||
// 1. Count the maximum element m in the array
|
||||
int m = 0;
|
||||
foreach (int num in nums) {
|
||||
m = Math.Max(m, num);
|
||||
}
|
||||
// 2. Count the occurrence of each number
|
||||
// counter[num] represents the occurrence of num
|
||||
int[] counter = new int[m + 1];
|
||||
foreach (int num in nums) {
|
||||
counter[num]++;
|
||||
}
|
||||
// 3. Calculate the prefix sum of counter, converting "occurrence count" to "tail index"
|
||||
// counter[num]-1 is the last index where num appears in res
|
||||
for (int i = 0; i < m; i++) {
|
||||
counter[i + 1] += counter[i];
|
||||
}
|
||||
// 4. Traverse nums in reverse order, placing each element into the result array res
|
||||
// Initialize the array res to record results
|
||||
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; // Place num at the corresponding index
|
||||
counter[num]--; // Decrement the prefix sum by 1, getting the next index to place num
|
||||
}
|
||||
// Use result array res to overwrite the original array 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("After counting sort (cannot sort objects) completes, nums = " + string.Join(" ", nums));
|
||||
|
||||
int[] nums1 = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4];
|
||||
CountingSort(nums1);
|
||||
Console.WriteLine("After counting sort completes, nums1 = " + string.Join(" ", nums));
|
||||
}
|
||||
}
|
||||
52
en/codes/csharp/chapter_sorting/heap_sort.cs
Normal file
52
en/codes/csharp/chapter_sorting/heap_sort.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* File: heap_sort.cs
|
||||
* Created Time: 2023-06-01
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class heap_sort {
|
||||
/* Heap length is n, start heapifying node i, from top to bottom */
|
||||
void SiftDown(int[] nums, int n, int i) {
|
||||
while (true) {
|
||||
// If node i is largest or indices l, r are out of bounds, no need to continue heapify, break
|
||||
int l = 2 * i + 1;
|
||||
int r = 2 * i + 2;
|
||||
int ma = i;
|
||||
if (l < n && nums[l] > nums[ma])
|
||||
ma = l;
|
||||
if (r < n && nums[r] > nums[ma])
|
||||
ma = r;
|
||||
// Swap two nodes
|
||||
if (ma == i)
|
||||
break;
|
||||
// Swap two nodes
|
||||
(nums[ma], nums[i]) = (nums[i], nums[ma]);
|
||||
// Loop downwards heapification
|
||||
i = ma;
|
||||
}
|
||||
}
|
||||
|
||||
/* Heap sort */
|
||||
void HeapSort(int[] nums) {
|
||||
// Build heap operation: heapify all nodes except leaves
|
||||
for (int i = nums.Length / 2 - 1; i >= 0; i--) {
|
||||
SiftDown(nums, nums.Length, i);
|
||||
}
|
||||
// Extract the largest element from the heap and repeat for n-1 rounds
|
||||
for (int i = nums.Length - 1; i > 0; i--) {
|
||||
// Delete node
|
||||
(nums[i], nums[0]) = (nums[0], nums[i]);
|
||||
// Start heapifying the root node, from top to bottom
|
||||
SiftDown(nums, i, 0);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = [4, 1, 3, 1, 5, 2];
|
||||
HeapSort(nums);
|
||||
Console.WriteLine("After heap sort completes, nums = " + string.Join(" ", nums));
|
||||
}
|
||||
}
|
||||
30
en/codes/csharp/chapter_sorting/insertion_sort.cs
Normal file
30
en/codes/csharp/chapter_sorting/insertion_sort.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* File: insertion_sort.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class insertion_sort {
|
||||
/* Insertion sort */
|
||||
void InsertionSort(int[] nums) {
|
||||
// Outer loop: sorted interval is [0, i-1]
|
||||
for (int i = 1; i < nums.Length; i++) {
|
||||
int bas = nums[i], j = i - 1;
|
||||
// Inner loop: insert base into the correct position within the sorted interval [0, i-1]
|
||||
while (j >= 0 && nums[j] > bas) {
|
||||
nums[j + 1] = nums[j]; // Move nums[j] to the right by one position
|
||||
j--;
|
||||
}
|
||||
nums[j + 1] = bas; // Assign base to the correct position
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = [4, 1, 3, 1, 5, 2];
|
||||
InsertionSort(nums);
|
||||
Console.WriteLine("After insertion sort completes, nums = " + string.Join(",", nums));
|
||||
}
|
||||
}
|
||||
56
en/codes/csharp/chapter_sorting/merge_sort.cs
Normal file
56
en/codes/csharp/chapter_sorting/merge_sort.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* File: merge_sort.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class merge_sort {
|
||||
/* Merge left subarray and right subarray */
|
||||
void Merge(int[] nums, int left, int mid, int right) {
|
||||
// Left subarray interval is [left, mid], right subarray interval is [mid+1, right]
|
||||
// Create a temporary array tmp to store the merged results
|
||||
int[] tmp = new int[right - left + 1];
|
||||
// Initialize the start indices of the left and right subarrays
|
||||
int i = left, j = mid + 1, k = 0;
|
||||
// While both subarrays still have elements, compare and copy the smaller element into the temporary array
|
||||
while (i <= mid && j <= right) {
|
||||
if (nums[i] <= nums[j])
|
||||
tmp[k++] = nums[i++];
|
||||
else
|
||||
tmp[k++] = nums[j++];
|
||||
}
|
||||
// Copy the remaining elements of the left and right subarrays into the temporary array
|
||||
while (i <= mid) {
|
||||
tmp[k++] = nums[i++];
|
||||
}
|
||||
while (j <= right) {
|
||||
tmp[k++] = nums[j++];
|
||||
}
|
||||
// Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval
|
||||
for (k = 0; k < tmp.Length; ++k) {
|
||||
nums[left + k] = tmp[k];
|
||||
}
|
||||
}
|
||||
|
||||
/* Merge sort */
|
||||
void MergeSort(int[] nums, int left, int right) {
|
||||
// Termination condition
|
||||
if (left >= right) return; // Terminate recursion when subarray length is 1
|
||||
// Divide and conquer stage
|
||||
int mid = left + (right - left) / 2; // Calculate midpoint
|
||||
MergeSort(nums, left, mid); // Recursively process the left subarray
|
||||
MergeSort(nums, mid + 1, right); // Recursively process the right subarray
|
||||
// Merge stage
|
||||
Merge(nums, left, mid, right);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Merge sort */
|
||||
int[] nums = [7, 3, 2, 6, 0, 1, 5, 4];
|
||||
MergeSort(nums, 0, nums.Length - 1);
|
||||
Console.WriteLine("After merge sort completes, nums = " + string.Join(",", nums));
|
||||
}
|
||||
}
|
||||
150
en/codes/csharp/chapter_sorting/quick_sort.cs
Normal file
150
en/codes/csharp/chapter_sorting/quick_sort.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
/**
|
||||
* File: quick_sort.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_sorting;
|
||||
|
||||
class quickSort {
|
||||
/* Swap elements */
|
||||
static void Swap(int[] nums, int i, int j) {
|
||||
(nums[j], nums[i]) = (nums[i], nums[j]);
|
||||
}
|
||||
|
||||
/* Sentinel partition */
|
||||
static int Partition(int[] nums, int left, int right) {
|
||||
// Use nums[left] as the pivot
|
||||
int i = left, j = right;
|
||||
while (i < j) {
|
||||
while (i < j && nums[j] >= nums[left])
|
||||
j--; // Search from right to left for the first element smaller than the pivot
|
||||
while (i < j && nums[i] <= nums[left])
|
||||
i++; // Search from left to right for the first element greater than the pivot
|
||||
Swap(nums, i, j); // Swap these two elements
|
||||
}
|
||||
Swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays
|
||||
return i; // Return the index of the pivot
|
||||
}
|
||||
|
||||
/* Quick sort */
|
||||
public static void QuickSort(int[] nums, int left, int right) {
|
||||
// Terminate recursion when subarray length is 1
|
||||
if (left >= right)
|
||||
return;
|
||||
// Sentinel partition
|
||||
int pivot = Partition(nums, left, right);
|
||||
// Recursively process the left subarray and right subarray
|
||||
QuickSort(nums, left, pivot - 1);
|
||||
QuickSort(nums, pivot + 1, right);
|
||||
}
|
||||
}
|
||||
|
||||
/* Quick sort class (median pivot optimization) */
|
||||
class QuickSortMedian {
|
||||
/* Swap elements */
|
||||
static void Swap(int[] nums, int i, int j) {
|
||||
(nums[j], nums[i]) = (nums[i], nums[j]);
|
||||
}
|
||||
|
||||
/* Select the median of three candidate elements */
|
||||
static int MedianThree(int[] nums, int left, int mid, int right) {
|
||||
int l = nums[left], m = nums[mid], r = nums[right];
|
||||
if ((l <= m && m <= r) || (r <= m && m <= l))
|
||||
return mid; // m is between l and r
|
||||
if ((m <= l && l <= r) || (r <= l && l <= m))
|
||||
return left; // l is between m and r
|
||||
return right;
|
||||
}
|
||||
|
||||
/* Sentinel partition (median of three) */
|
||||
static int Partition(int[] nums, int left, int right) {
|
||||
// Select the median of three candidate elements
|
||||
int med = MedianThree(nums, left, (left + right) / 2, right);
|
||||
// Swap the median to the array's leftmost position
|
||||
Swap(nums, left, med);
|
||||
// Use nums[left] as the pivot
|
||||
int i = left, j = right;
|
||||
while (i < j) {
|
||||
while (i < j && nums[j] >= nums[left])
|
||||
j--; // Search from right to left for the first element smaller than the pivot
|
||||
while (i < j && nums[i] <= nums[left])
|
||||
i++; // Search from left to right for the first element greater than the pivot
|
||||
Swap(nums, i, j); // Swap these two elements
|
||||
}
|
||||
Swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays
|
||||
return i; // Return the index of the pivot
|
||||
}
|
||||
|
||||
/* Quick sort */
|
||||
public static void QuickSort(int[] nums, int left, int right) {
|
||||
// Terminate recursion when subarray length is 1
|
||||
if (left >= right)
|
||||
return;
|
||||
// Sentinel partition
|
||||
int pivot = Partition(nums, left, right);
|
||||
// Recursively process the left subarray and right subarray
|
||||
QuickSort(nums, left, pivot - 1);
|
||||
QuickSort(nums, pivot + 1, right);
|
||||
}
|
||||
}
|
||||
|
||||
/* Quick sort class (recursion depth optimization) */
|
||||
class QuickSortTailCall {
|
||||
/* Swap elements */
|
||||
static void Swap(int[] nums, int i, int j) {
|
||||
(nums[j], nums[i]) = (nums[i], nums[j]);
|
||||
}
|
||||
|
||||
/* Sentinel partition */
|
||||
static int Partition(int[] nums, int left, int right) {
|
||||
// Use nums[left] as the pivot
|
||||
int i = left, j = right;
|
||||
while (i < j) {
|
||||
while (i < j && nums[j] >= nums[left])
|
||||
j--; // Search from right to left for the first element smaller than the pivot
|
||||
while (i < j && nums[i] <= nums[left])
|
||||
i++; // Search from left to right for the first element greater than the pivot
|
||||
Swap(nums, i, j); // Swap these two elements
|
||||
}
|
||||
Swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays
|
||||
return i; // Return the index of the pivot
|
||||
}
|
||||
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
public static void QuickSort(int[] nums, int left, int right) {
|
||||
// Terminate when subarray length is 1
|
||||
while (left < right) {
|
||||
// Sentinel partition operation
|
||||
int pivot = Partition(nums, left, right);
|
||||
// Perform quick sort on the shorter of the two subarrays
|
||||
if (pivot - left < right - pivot) {
|
||||
QuickSort(nums, left, pivot - 1); // Recursively sort the left subarray
|
||||
left = pivot + 1; // Remaining unsorted interval is [pivot + 1, right]
|
||||
} else {
|
||||
QuickSort(nums, pivot + 1, right); // Recursively sort the right subarray
|
||||
right = pivot - 1; // Remaining unsorted interval is [left, pivot - 1]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class quick_sort {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Quick sort */
|
||||
int[] nums = [2, 4, 1, 0, 3, 5];
|
||||
quickSort.QuickSort(nums, 0, nums.Length - 1);
|
||||
Console.WriteLine("After quick sort completes, nums = " + string.Join(",", nums));
|
||||
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
int[] nums1 = [2, 4, 1, 0, 3, 5];
|
||||
QuickSortMedian.QuickSort(nums1, 0, nums1.Length - 1);
|
||||
Console.WriteLine("After quick sort (median pivot optimization) completes, nums1 = " + string.Join(",", nums1));
|
||||
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
int[] nums2 = [2, 4, 1, 0, 3, 5];
|
||||
QuickSortTailCall.QuickSort(nums2, 0, nums2.Length - 1);
|
||||
Console.WriteLine("After quick sort (recursion depth optimization) completes, nums2 = " + string.Join(",", nums2));
|
||||
}
|
||||
}
|
||||
69
en/codes/csharp/chapter_sorting/radix_sort.cs
Normal file
69
en/codes/csharp/chapter_sorting/radix_sort.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* File: radix_sort.cs
|
||||
* Created Time: 2023-04-13
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class radix_sort {
|
||||
/* Get the k-th digit of element num, where exp = 10^(k-1) */
|
||||
int Digit(int num, int exp) {
|
||||
// Passing exp instead of k can avoid repeated expensive exponentiation here
|
||||
return (num / exp) % 10;
|
||||
}
|
||||
|
||||
/* Counting sort (based on nums k-th digit) */
|
||||
void CountingSortDigit(int[] nums, int exp) {
|
||||
// Decimal digit range is 0~9, therefore need a bucket array of length 10
|
||||
int[] counter = new int[10];
|
||||
int n = nums.Length;
|
||||
// Count the occurrence of digits 0~9
|
||||
for (int i = 0; i < n; i++) {
|
||||
int d = Digit(nums[i], exp); // Get the k-th digit of nums[i], noted as d
|
||||
counter[d]++; // Count the occurrence of digit d
|
||||
}
|
||||
// Calculate prefix sum, converting "occurrence count" into "array index"
|
||||
for (int i = 1; i < 10; i++) {
|
||||
counter[i] += counter[i - 1];
|
||||
}
|
||||
// Traverse in reverse, based on bucket statistics, place each element into 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; // Get the index j for d in the array
|
||||
res[j] = nums[i]; // Place the current element at index j
|
||||
counter[d]--; // Decrease the count of d by 1
|
||||
}
|
||||
// Use result to overwrite the original array nums
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums[i] = res[i];
|
||||
}
|
||||
}
|
||||
|
||||
/* Radix sort */
|
||||
void RadixSort(int[] nums) {
|
||||
// Get the maximum element of the array, used to determine the maximum number of digits
|
||||
int m = int.MinValue;
|
||||
foreach (int num in nums) {
|
||||
if (num > m) m = num;
|
||||
}
|
||||
// Traverse from the lowest to the highest digit
|
||||
for (int exp = 1; exp <= m; exp *= 10) {
|
||||
// Perform counting sort on the k-th digit of array elements
|
||||
// k = 1 -> exp = 1
|
||||
// k = 2 -> exp = 10
|
||||
// i.e., exp = 10^(k-1)
|
||||
CountingSortDigit(nums, exp);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
// Radix sort
|
||||
int[] nums = [ 10546151, 35663510, 42865989, 34862445, 81883077,
|
||||
88906420, 72429244, 30524779, 82060337, 63832996 ];
|
||||
RadixSort(nums);
|
||||
Console.WriteLine("After radix sort completes, nums = " + string.Join(" ", nums));
|
||||
}
|
||||
}
|
||||
32
en/codes/csharp/chapter_sorting/selection_sort.cs
Normal file
32
en/codes/csharp/chapter_sorting/selection_sort.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* File: selection_sort.cs
|
||||
* Created Time: 2023-06-01
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_sorting;
|
||||
|
||||
public class selection_sort {
|
||||
/* Selection sort */
|
||||
void SelectionSort(int[] nums) {
|
||||
int n = nums.Length;
|
||||
// Outer loop: unsorted interval is [i, n-1]
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
// Inner loop: find the smallest element within the unsorted interval
|
||||
int k = i;
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
if (nums[j] < nums[k])
|
||||
k = j; // Record the index of the smallest element
|
||||
}
|
||||
// Swap the smallest element with the first element of the unsorted interval
|
||||
(nums[k], nums[i]) = (nums[i], nums[k]);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = [4, 1, 3, 1, 5, 2];
|
||||
SelectionSort(nums);
|
||||
Console.WriteLine("After selection sort completes, nums = " + string.Join(" ", nums));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user