Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add c# insert in InsertionSort #689

Merged
merged 3 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions basic/sorting/InsertionSort/InsertionSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Diagnostics;
using static System.Console;
namespace Pro;
public class Program
{
public static void Main()
{
int[] test = new int[]{31, 12, 10, 5, 6, 7, 8, 10, 23, 34, 56, 43, 32, 21};
InsertSortNums(test);
foreach (var item in test)
{
WriteLine(item);
}
}
public static void InsertSortNums(int[] nums)
{
for(int initial = 1; initial < nums.Length; initial++)
{
for(int second_sort = 0; second_sort < initial; second_sort++)
{
if(nums[second_sort] > nums[initial])
{
swap(ref nums[second_sort], ref nums[initial]);
}
}
}
}

private static void swap(ref int compare_left, ref int compare_right)
{
int temp = compare_left;
compare_left = compare_right;
compare_right = temp;
}
}
40 changes: 40 additions & 0 deletions basic/sorting/InsertionSort/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,46 @@ fn main() {
}
```

### **C#**

```cs
using System.Diagnostics;
using static System.Console;
namespace Pro;
public class Program
{
public static void Main()
{
int[] test = new int[]{31, 12, 10, 5, 6, 7, 8, 10, 23, 34, 56, 43, 32, 21};
InsertSortNums(test);
foreach (var item in test)
{
WriteLine(item);
}
}
public static void InsertSortNums(int[] nums)
{
for(int initial = 1; initial < nums.Length; initial++)
{
for(int second_sort = 0; second_sort < initial; second_sort++)
{
if(nums[second_sort] > nums[initial])
{
swap(ref nums[second_sort], ref nums[initial]);
}
}
}
}

private static void swap(ref int compare_left, ref int compare_right)
{
int temp = compare_left;
compare_left = compare_right;
compare_right = temp;
}
}
```

<!-- tabs:end -->

## 算法分析
Expand Down