Skip to content

Commit e20ec94

Browse files
authored
feat: add c# code in SelectionSort (#691)
1 parent 1b12dcc commit e20ec94

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

basic/sorting/SelectionSort/README.md

+42
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,48 @@ fn main() {
163163
}
164164
```
165165

166+
### **C#**
167+
168+
```cs
169+
using static System.Console;
170+
namespace Pro;
171+
public class Program
172+
{
173+
public static void Main()
174+
{
175+
int[] test = new int[] {90, 12, 77, 9, 0, 2, 23, 23, 3, 57, 80};
176+
SelectionSortNums(test);
177+
foreach (var item in test)
178+
{
179+
WriteLine(item);
180+
}
181+
}
182+
public static void SelectionSortNums(int[] nums)
183+
{
184+
for (int initial = 0; initial < nums.Length; initial++)
185+
{
186+
for (int second_sort = initial; second_sort < nums.Length; second_sort++)
187+
{
188+
if (nums[initial] > nums[second_sort])
189+
{
190+
swap(ref nums[initial], ref nums[second_sort]);
191+
}
192+
}
193+
}
194+
195+
}
196+
197+
private static void swap(ref int compare_left, ref int compare_right)
198+
{
199+
int temp = compare_left;
200+
compare_left = compare_right;
201+
compare_right = temp;
202+
}
203+
204+
}
205+
206+
```
207+
166208
<!-- tabs:end -->
167209

168210
## 算法分析
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using static System.Console;
2+
namespace Pro;
3+
public class Program
4+
{
5+
public static void Main()
6+
{
7+
int[] test = new int[] {90, 12, 77, 9, 0, 2, 23, 23, 3, 57, 80};
8+
SelectionSortNums(test);
9+
foreach (var item in test)
10+
{
11+
WriteLine(item);
12+
}
13+
}
14+
public static void SelectionSortNums(int[] nums)
15+
{
16+
for (int initial = 0; initial < nums.Length; initial++)
17+
{
18+
for (int second_sort = initial; second_sort < nums.Length; second_sort++)
19+
{
20+
if (nums[initial] > nums[second_sort])
21+
{
22+
swap(ref nums[initial], ref nums[second_sort]);
23+
}
24+
}
25+
}
26+
27+
}
28+
29+
private static void swap(ref int compare_left, ref int compare_right)
30+
{
31+
int temp = compare_left;
32+
compare_left = compare_right;
33+
compare_right = temp;
34+
}
35+
36+
}

0 commit comments

Comments
 (0)