정렬되지 않은 전체 자료 중에서 해당 위치에 맞는 자료를 선택하여 위치를 교환하는 정렬 방식

같은 값의 인덱스끼리도 교환 연산이 발생하기때문에 안전성을 만족하지 않고 전체적인 알고리즘 효율성을 볼 때 느림

 

 - 시간복잡도 (Big-O notation)  


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
using System.Collections.Generic;
 
    public enum SortType
    {
        Asc,
        Desc
    }
 
    public static void SelectionSort(int[] array, SortType st)
    {
        int length = array.Length;
 
        int key;
        int tmp;
 
        if (st == SortType.Asc)
        {
            // Asc
            for (int i = 0; i < length - 1++i)
            {
                key = i;
 
                for (int j = i + 1; j < length; ++j)
                {
                    if (array[key] > array[j])
                    {
                        tmp = array[j];
                        array[j] = array[key];
                        array[key] = tmp;
                    }
                }
            }
        }
        else
        {
            // Desc
            for (int i = 0; i < length - 1++i)
            {
                key = i;
 
                for (int j = i + 1; j < length; ++j)
                {
                    if (array[key] < array[j])
                    {
                        tmp = array[j];
                        array[j] = array[key];
                        array[key] = tmp;
                    }
                }
            }
        }
    }
cs


대학교 4학년 수업시간에

'Switch N Light'라는 입사시험문제를 풀면서 만들었던 BackTracking 알고리즘 관련 ppt자료입니다.















'02.Development > Algorithm' 카테고리의 다른 글

[알고리즘] 선택정렬 (C#)  (0) 2016.02.04

+ Recent posts