Skip to content

Commit 04ba8de

Browse files
committed
Added c# version of Selection Sort algorithm
1 parent 20e9877 commit 04ba8de

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Sorting/Selection Sort/C#/Program.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
5+
namespace Algorithms
6+
{
7+
class Sort
8+
{
9+
static void Selection(int[] numbers)
10+
{
11+
for (int i = 0; i < numbers.Length - 1; i++)
12+
{
13+
int minIndex = i;
14+
15+
for (int j = i + 1; j < numbers.Length; j++)
16+
{
17+
if (numbers[j] < numbers[minIndex]) minIndex = j;
18+
}
19+
20+
if (minIndex != i)
21+
{
22+
Swap(ref numbers[minIndex], ref numbers[i]);
23+
}
24+
}
25+
}
26+
27+
static void Swap(ref int a, ref int b)
28+
{
29+
int temp = a;
30+
a = b;
31+
b = temp;
32+
}
33+
34+
static void Main(string[] args)
35+
{
36+
int[] numbers = {5, 1, 7, 6, -4, 30, 2};
37+
38+
Selection(numbers);
39+
40+
foreach (int item in numbers)
41+
{
42+
Console.WriteLine(item.ToString());
43+
}
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)