-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectionSort.cs
40 lines (35 loc) · 904 Bytes
/
SelectionSort.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
class SelectionSort
{
static void Main()
{
Console.Write("Enter array length:");
int n = int.Parse(Console.ReadLine());
int[] arr = new int[n];
int min = 0;
for (int i = 0; i < arr.Length; i++)
{
Console.Write("arr[{0}]=", i + 1);
arr[i] = int.Parse(Console.ReadLine());
}
for (int i = 0; i < arr.Length - 1; i++)
{
min = i;
for (int q = i + 1; q < arr.Length; q++)
{
if (arr[min] > arr[q]) min = q;
}
if (min != i)
{
int k = arr[i];
arr[i] = arr[min];
arr[min] = k;
}
}
Console.WriteLine("Results:");
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
}
}