Re-translate the Japanese version (#1871)

* Retranslate Japanese docs with GPT-5.4

* Retranslate Japanese code with GPT-5.4
This commit is contained in:
Yudong Jin
2026-03-30 07:30:15 +08:00
committed by GitHub
parent fe6443235b
commit d7b2277d2b
1444 changed files with 83312 additions and 8363 deletions
@@ -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 {
/* バブルソート */
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] を交換
(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);
}
}
}
}
/* バブルソート(フラグ最適化) */
void BubbleSortWithFlag(int[] nums) {
// 外側のループ:未ソート区間は [0, i]
for (int i = nums.Length - 1; i > 0; i--) {
bool flag = false; // フラグを初期化する
// 内側のループ:未ソート区間 [0, i] の最大要素をその区間の最右端へ交換
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// nums[j] と nums[j + 1] を交換
(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);
flag = true; // 交換する要素を記録
}
}
if (!flag) break; // このバブル処理で要素交換が一度もなければそのまま終了
}
}
[Test]
public void Test() {
int[] nums = [4, 1, 3, 1, 5, 2];
BubbleSort(nums);
Console.WriteLine("バブルソート完了後 nums = " + string.Join(",", nums));
int[] nums1 = [4, 1, 3, 1, 5, 2];
BubbleSortWithFlag(nums1);
Console.WriteLine("バブルソート完了後 nums1 = " + string.Join(",", nums1));
}
}
@@ -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 {
/* バケットソート */
void BucketSort(float[] nums) {
// k = n/2 個のバケットを初期化し、各バケットに 2 要素ずつ割り当てる想定とする
int k = nums.Length / 2;
List<List<float>> buckets = [];
for (int i = 0; i < k; i++) {
buckets.Add([]);
}
// 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));
}
}
@@ -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 {
/* 計数ソート */
// 簡易実装のため、オブジェクトのソートには使えない
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;
}
}
}
/* 計数ソート */
// 完全な実装で、オブジェクトをソートでき、かつ安定ソートである
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));
}
}
@@ -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 {
/* ヒープの長さは n。ノード i から下方向にヒープ化 */
void SiftDown(int[] nums, int n, int i) {
while (true) {
// ノード i, l, r のうち値が最大のノードを ma とする
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;
// ノード i が最大、またはインデックス l, r が範囲外なら、ヒープ化は不要なので抜ける
if (ma == i)
break;
// 2 つのノードを交換
(nums[ma], nums[i]) = (nums[i], nums[ma]);
// ループで上から下へヒープ化
i = ma;
}
}
/* ヒープソート */
void HeapSort(int[] nums) {
// ヒープ構築:葉ノード以外のすべてのノードをヒープ化する
for (int i = nums.Length / 2 - 1; i >= 0; 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);
}
}
[Test]
public void Test() {
int[] nums = [4, 1, 3, 1, 5, 2];
HeapSort(nums);
Console.WriteLine("ヒープソート完了後 nums = " + string.Join(" ", nums));
}
}
@@ -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 {
/* 挿入ソート */
void InsertionSort(int[] nums) {
// 外側ループ:整列済み区間は [0, i-1]
for (int i = 1; i < nums.Length; i++) {
int bas = nums[i], j = i - 1;
// 内側ループ: base をソート済み区間 [0, i-1] の正しい位置に挿入する
while (j >= 0 && nums[j] > bas) {
nums[j + 1] = nums[j]; // nums[j] を 1 つ右へ移動する
j--;
}
nums[j + 1] = bas; // base を正しい位置に配置する
}
}
[Test]
public void Test() {
int[] nums = [4, 1, 3, 1, 5, 2];
InsertionSort(nums);
Console.WriteLine("挿入ソート完了後 nums = " + string.Join(",", nums));
}
}
@@ -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 {
/* 左部分配列と右部分配列をマージ */
void Merge(int[] nums, int left, int mid, int right) {
// 左部分配列の区間は [left, mid]、右部分配列の区間は [mid+1, right]
// マージ結果を格納する一時配列 tmp を作成
int[] tmp = new int[right - left + 1];
// 左右の部分配列の開始インデックスを初期化する
int i = left, j = mid + 1, k = 0;
// 左右の部分配列にまだ要素がある間は比較し、小さいほうを一時配列にコピーする
while (i <= mid && j <= right) {
if (nums[i] <= nums[j])
tmp[k++] = nums[i++];
else
tmp[k++] = nums[j++];
}
// 左右の部分配列の残り要素を一時配列にコピーする
while (i <= mid) {
tmp[k++] = nums[i++];
}
while (j <= right) {
tmp[k++] = nums[j++];
}
// 一時配列 tmp の要素を元の配列 nums の対応区間にコピーする
for (k = 0; k < tmp.Length; ++k) {
nums[left + k] = tmp[k];
}
}
/* マージソート */
void MergeSort(int[] nums, int left, int right) {
// 終了条件
if (left >= right) return; // 部分配列の長さが 1 になったら再帰を終了
// 分割フェーズ
int mid = left + (right - left) / 2; // 中点を計算
MergeSort(nums, left, mid); // 左部分配列を再帰処理
MergeSort(nums, mid + 1, 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);
Console.WriteLine("マージソート完了後 nums = " + string.Join(",", nums));
}
}
@@ -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 {
/* 要素の交換 */
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) {
// nums[left] を基準値とする
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
j--; // 右から左へ基準値未満の最初の要素を探す
while (i < j && nums[i] <= nums[left])
i++; // 左から右へ基準値より大きい最初の要素を探す
Swap(nums, i, j); // この 2 つの要素を交換
}
Swap(nums, i, left); // 基準値を 2 つの部分配列の境界へ交換する
return i; // 基準値のインデックスを返す
}
/* クイックソート */
public static void QuickSort(int[] nums, int left, int right) {
// 部分配列の長さが 1 なら再帰を終了する
if (left >= right)
return;
// 番兵分割
int pivot = Partition(nums, left, right);
// 左右の部分配列を再帰処理
QuickSort(nums, left, pivot - 1);
QuickSort(nums, pivot + 1, right);
}
}
/* クイックソートクラス(中央値ピボット最適化) */
class QuickSortMedian {
/* 要素の交換 */
static void Swap(int[] nums, int i, int j) {
(nums[j], nums[i]) = (nums[i], nums[j]);
}
/* 3つの候補要素の中央値を選ぶ */
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 は l と r の間
if ((m <= l && l <= r) || (r <= l && l <= m))
return left; // l は m と r の間
return right;
}
/* 番兵による分割処理(3 点中央値) */
static int Partition(int[] nums, int left, int right) {
// 3つの候補要素の中央値を選ぶ
int med = MedianThree(nums, left, (left + right) / 2, right);
// 中央値を配列の最左端に交換する
Swap(nums, left, med);
// nums[left] を基準値とする
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
j--; // 右から左へ基準値未満の最初の要素を探す
while (i < j && nums[i] <= nums[left])
i++; // 左から右へ基準値より大きい最初の要素を探す
Swap(nums, i, j); // この 2 つの要素を交換
}
Swap(nums, i, left); // 基準値を 2 つの部分配列の境界へ交換する
return i; // 基準値のインデックスを返す
}
/* クイックソート */
public static void QuickSort(int[] nums, int left, int right) {
// 部分配列の長さが 1 なら再帰を終了する
if (left >= right)
return;
// 番兵分割
int pivot = Partition(nums, left, right);
// 左右の部分配列を再帰処理
QuickSort(nums, left, pivot - 1);
QuickSort(nums, pivot + 1, right);
}
}
/* クイックソートクラス(再帰深度最適化) */
class QuickSortTailCall {
/* 要素の交換 */
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) {
// nums[left] を基準値とする
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
j--; // 右から左へ基準値未満の最初の要素を探す
while (i < j && nums[i] <= nums[left])
i++; // 左から右へ基準値より大きい最初の要素を探す
Swap(nums, i, j); // この 2 つの要素を交換
}
Swap(nums, i, left); // 基準値を 2 つの部分配列の境界へ交換する
return i; // 基準値のインデックスを返す
}
/* クイックソート(再帰深度最適化) */
public static void QuickSort(int[] nums, int left, int right) {
// 部分配列の長さが 1 なら終了
while (left < right) {
// 番兵による分割処理
int pivot = Partition(nums, left, right);
// 2 つの部分配列のうち短いほうにクイックソートを適用する
if (pivot - left < right - pivot) {
QuickSort(nums, left, pivot - 1); // 左部分配列を再帰的にソート
left = pivot + 1; // 未ソート区間の残りは [pivot + 1, right]
} else {
QuickSort(nums, pivot + 1, right); // 右部分配列を再帰的にソート
right = pivot - 1; // 未ソート区間の残りは [left, pivot - 1]
}
}
}
}
public class quick_sort {
[Test]
public void Test() {
/* クイックソート */
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];
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);
Console.WriteLine("クイックソート(再帰深度最適化)完了後 nums2 = " + string.Join(",", nums2));
}
}
@@ -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 {
/* 要素 num の下から k 桁目を取得(exp = 10^(k-1) */
int Digit(int num, int exp) {
// ここで高コストな累乗計算を繰り返さないよう、k ではなく exp を渡す
return (num / exp) % 10;
}
/* 計数ソート(nums の k 桁目でソート) */
void CountingSortDigit(int[] nums, int exp) {
// 10 進数の各桁は 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];
}
}
/* 基数ソート */
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));
}
}
@@ -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 {
/* 選択ソート */
void SelectionSort(int[] nums) {
int n = nums.Length;
// 外側ループ:未整列区間は [i, n-1]
for (int i = 0; i < n - 1; i++) {
// 内側のループ:未ソート区間の最小要素を見つける
int k = i;
for (int j = i + 1; j < n; j++) {
if (nums[j] < nums[k])
k = j; // 最小要素のインデックスを記録
}
// その最小要素を未整列区間の先頭要素と交換する
(nums[k], nums[i]) = (nums[i], nums[k]);
}
}
[Test]
public void Test() {
int[] nums = [4, 1, 3, 1, 5, 2];
SelectionSort(nums);
Console.WriteLine("選択ソート完了後 nums = " + string.Join(" ", nums));
}
}