From 361cdc26b2ca8f9b50febcf6661dc22bc676f859 Mon Sep 17 00:00:00 2001 From: PeixiZ Date: Wed, 26 Jan 2022 16:57:27 +0800 Subject: [PATCH 1/3] add c# select --- basic/sorting/SelectionSort/SelectionSort.cs | 36 ++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 basic/sorting/SelectionSort/SelectionSort.cs diff --git a/basic/sorting/SelectionSort/SelectionSort.cs b/basic/sorting/SelectionSort/SelectionSort.cs new file mode 100644 index 0000000000000..93a374da2c0b7 --- /dev/null +++ b/basic/sorting/SelectionSort/SelectionSort.cs @@ -0,0 +1,36 @@ +using static System.Console; +namespace Pro; +public class Program +{ + public static void Main() + { + int[] test = new int[] {90, 12, 77, 9, 0, 2, 23, 23, 3, 57, 80}; + SelectionSortNums(test); + foreach (var item in test) + { + WriteLine(item); + } + } + public static void SelectionSortNums(int[] nums) + { + for (int initial = 0; initial < nums.Length; initial++) + { + for (int second_sort = initial; second_sort < nums.Length; second_sort++) + { + if(nums[initial] > nums[second_sort]) + { + swap(ref nums[initial], ref nums[second_sort]); + } + } + } + + } + + private static void swap(ref int compare_left, ref int compare_right) + { + int temp = compare_left; + compare_left = compare_right; + compare_right = temp; + } + +} From b61c13399456d0c8f0cc1c963fea07b59bbb5380 Mon Sep 17 00:00:00 2001 From: PeixiZ Date: Wed, 26 Jan 2022 17:01:57 +0800 Subject: [PATCH 2/3] fix readme --- basic/sorting/SelectionSort/README.md | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/basic/sorting/SelectionSort/README.md b/basic/sorting/SelectionSort/README.md index 800da7398e81c..cb8c16eb1a3e6 100644 --- a/basic/sorting/SelectionSort/README.md +++ b/basic/sorting/SelectionSort/README.md @@ -163,6 +163,48 @@ fn main() { } ``` +### **C#** + +```cs +using static System.Console; +namespace Pro; +public class Program +{ + public static void Main() + { + int[] test = new int[] {90, 12, 77, 9, 0, 2, 23, 23, 3, 57, 80}; + SelectionSortNums(test); + foreach (var item in test) + { + WriteLine(item); + } + } + public static void SelectionSortNums(int[] nums) + { + for (int initial = 0; initial < nums.Length; initial++) + { + for (int second_sort = initial; second_sort < nums.Length; second_sort++) + { + if(nums[initial] > nums[second_sort]) + { + swap(ref nums[initial], ref nums[second_sort]); + } + } + } + + } + + private static void swap(ref int compare_left, ref int compare_right) + { + int temp = compare_left; + compare_left = compare_right; + compare_right = temp; + } + +} + +``` + ## 算法分析 From b8ce1e15cff079cba5d876b00c5ea11b0e4f85b2 Mon Sep 17 00:00:00 2001 From: PeixiZ Date: Wed, 26 Jan 2022 17:19:01 +0800 Subject: [PATCH 3/3] fix format --- basic/sorting/SelectionSort/README.md | 2 +- basic/sorting/SelectionSort/SelectionSort.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/basic/sorting/SelectionSort/README.md b/basic/sorting/SelectionSort/README.md index cb8c16eb1a3e6..e5ec027b2c5a9 100644 --- a/basic/sorting/SelectionSort/README.md +++ b/basic/sorting/SelectionSort/README.md @@ -185,7 +185,7 @@ public class Program { for (int second_sort = initial; second_sort < nums.Length; second_sort++) { - if(nums[initial] > nums[second_sort]) + if (nums[initial] > nums[second_sort]) { swap(ref nums[initial], ref nums[second_sort]); } diff --git a/basic/sorting/SelectionSort/SelectionSort.cs b/basic/sorting/SelectionSort/SelectionSort.cs index 93a374da2c0b7..f92e3679d7544 100644 --- a/basic/sorting/SelectionSort/SelectionSort.cs +++ b/basic/sorting/SelectionSort/SelectionSort.cs @@ -17,7 +17,7 @@ public static void SelectionSortNums(int[] nums) { for (int second_sort = initial; second_sort < nums.Length; second_sort++) { - if(nums[initial] > nums[second_sort]) + if (nums[initial] > nums[second_sort]) { swap(ref nums[initial], ref nums[second_sort]); }