Skip to content

Commit 4061c1c

Browse files
authored
feat: add c# code in InsertionSort (doocs#689)
1 parent 487a5fc commit 4061c1c

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Diagnostics;
2+
using static System.Console;
3+
namespace Pro;
4+
public class Program
5+
{
6+
public static void Main()
7+
{
8+
int[] test = new int[]{31, 12, 10, 5, 6, 7, 8, 10, 23, 34, 56, 43, 32, 21};
9+
InsertSortNums(test);
10+
foreach (var item in test)
11+
{
12+
WriteLine(item);
13+
}
14+
}
15+
public static void InsertSortNums(int[] nums)
16+
{
17+
for(int initial = 1; initial < nums.Length; initial++)
18+
{
19+
for(int second_sort = 0; second_sort < initial; second_sort++)
20+
{
21+
if(nums[second_sort] > nums[initial])
22+
{
23+
swap(ref nums[second_sort], ref nums[initial]);
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+
}

basic/sorting/InsertionSort/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,46 @@ fn main() {
157157
}
158158
```
159159

160+
### **C#**
161+
162+
```cs
163+
using System.Diagnostics;
164+
using static System.Console;
165+
namespace Pro;
166+
public class Program
167+
{
168+
public static void Main()
169+
{
170+
int[] test = new int[]{31, 12, 10, 5, 6, 7, 8, 10, 23, 34, 56, 43, 32, 21};
171+
InsertSortNums(test);
172+
foreach (var item in test)
173+
{
174+
WriteLine(item);
175+
}
176+
}
177+
public static void InsertSortNums(int[] nums)
178+
{
179+
for(int initial = 1; initial < nums.Length; initial++)
180+
{
181+
for(int second_sort = 0; second_sort < initial; second_sort++)
182+
{
183+
if(nums[second_sort] > nums[initial])
184+
{
185+
swap(ref nums[second_sort], ref nums[initial]);
186+
}
187+
}
188+
}
189+
}
190+
191+
private static void swap(ref int compare_left, ref int compare_right)
192+
{
193+
int temp = compare_left;
194+
compare_left = compare_right;
195+
compare_right = temp;
196+
}
197+
}
198+
```
199+
160200
<!-- tabs:end -->
161201

162202
## 算法分析

0 commit comments

Comments
 (0)