Skip to content

add C# selectsort in SelectionSort #690

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

Closed
wants to merge 2 commits into from
Closed
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])
{
Comment on lines +188 to +189
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(nums[initial] > nums[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])
{
Comment on lines +20 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(nums[initial] > nums[second_sort])
{
if (nums[initial] > nums[second_sort])
{

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if 语句这里也稍微改一下

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

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

}