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# select in SelectionSort #691

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
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;
}

}