Skip to content

Commit

Permalink
feat: add c# code in SelectionSort (doocs#691)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeixiZ authored Jan 26, 2022
1 parent 1b12dcc commit e20ec94
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
42 changes: 42 additions & 0 deletions basic/sorting/SelectionSort/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}

```

<!-- tabs:end -->

## 算法分析
Expand Down
36 changes: 36 additions & 0 deletions basic/sorting/SelectionSort/SelectionSort.cs
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit e20ec94

Please sign in to comment.