Algorithm
선택정렬 (Selection Sort) C#
일등하이
2020. 10. 23. 08:33
반응형
using System;
namespace SelectSort {
class Program {
static void Main (string[] args) {
int[] arr = { 4, 1, 5, 3, 2 };
int smallest;
int temp;
for (int i = 0; i < arr.Length - 1; i++) {
smallest = i;
for (int j = i + 1; j < arr.Length; j++) {
if (arr[j] < arr[smallest]) {
smallest = j;
}
}
temp = arr[smallest];
arr[smallest] = arr[i];
arr[i] = temp;
}
foreach (var num in arr) {
Console.WriteLine (num);
}
}
}
}
반응형