diff --git a/basic/sorting/SelectionSort/README.md b/basic/sorting/SelectionSort/README.md index 800da7398e81c..e5ec027b2c5a9 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; + } + +} + +``` + ## 算法分析 diff --git a/basic/sorting/SelectionSort/SelectionSort.cs b/basic/sorting/SelectionSort/SelectionSort.cs new file mode 100644 index 0000000000000..f92e3679d7544 --- /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; + } + +}