-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSortAlgorithm.cs
More file actions
55 lines (49 loc) · 1.54 KB
/
SelectionSortAlgorithm.cs
File metadata and controls
55 lines (49 loc) · 1.54 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
namespace SelectionSortAlgorithm
{
class SelectionSortAlgorithm
{
static void SelectionSort(int[] data)
{
for(int i = 0; i < data.Length; i++)
{
int min = i;
for(int j = i + 1; j < data.Length; j++)
{
if(data[j] < data[min])
{
min = j;
}
}
int temp = data[i];
data[i] = data[min];
data[min] = temp;
}
}
static void Main(string[] args)
{
Console.Write("Enter the size of the array: ");
int n = int.Parse(Console.ReadLine());
// Create the array
int[] arr = new int[n];
Console.WriteLine("Enter the corresponding values of the array");
for(int i = 0; i < n; i++)
{
Console.Write("Array[{0}] = ", i);
arr[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine(DateTime.Now);
// sort the array
SelectionSort(arr);
// print the sorted array
Console.WriteLine("Sorted array");
for(int i = 0; i < arr.Length; i++)
{
Console.WriteLine(" " + arr[i]);
}
Console.WriteLine();
Console.WriteLine(DateTime.Now);
Console.Read();
}
}
}