반응형
- 데이타의 양이 적을 때 유리함
- 구현이 쉽다.
- 추가 저장 공간이 필요 없음 (제자리 정렬)
방법
1. 목록에서 최소값을 찾는다.
2. 찾은 최소값을 맨 앞의 값과 교체
3. 정렬이 완료 될때까지 반복
using System;
namespace SelectionSort
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Selection sort");
int[] numbers = { 2, 9, 3, 7, 1, 4, 6, 5, 8 };
Print(numbers);
SelectionSort(numbers);
Print(numbers);
}
static void Print(int[] a)
{
string temp = "";
for (int i = 0 ;i < a.Length; i++)
temp += a[i].ToString();
Console.WriteLine(temp);
}
static void SelectionSort(int[] a)
{
int total = a.Length;
for (int i = 0; i < total; i++)
{
int min = i;
for (int j=i+1; j<total; j++)
{
if (a[j] < a[min])
{
min = j;
}
}
int temp = a[min];
a[min] = a[i];
a[i] = temp;
}
}
}
}
결과
반응형
'Programing > C#' 카테고리의 다른 글
복리 계산기 (0) | 2021.02.16 |
---|---|
Bubble Sort(버블 정렬) (0) | 2021.02.15 |
do while의 활용 법 featuring ReadAsStreamAsync(비동기 스트림) (0) | 2020.02.18 |
double vs decimal (0) | 2020.02.11 |
c# reflection 을 이용해서 assembly의 type과 method의 갯수 파악하기 (0) | 2020.02.11 |