<선택정렬 알고리즘>수정1

Algorithmus 2019. 3. 29. 09:38

선택정렬 알고리즘 - 오름차순/내림차순, 스왑 메서드화

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190329
{
    class SelectSort
    {
        public List<int> list = new List<int> { 12605 };
 
        public void AscendingSelectSort()//오름차순 정렬
        {
            int i = 0;
            int j = 0;
            int min = 0//최소값의 index를 확인할 아이
 
            for (i = 0; i < list.Count - 1; i++)//전체 훑기 Count-1 : 마지막칸은 자동으로 정렬되고 뒷칸이 없으므로 제외
            {
                min = i;//최소값을 들고 있을 제 3의 손//맨 처음엔 검사하는 첫배열을 들고있음
 
                Console.WriteLine("\n{0}회정렬 전", i);
                for (int a = 0; a < list.Count; a++)
                {
                    Console.Write("{0} ", list[a]);
                }
                for (j = i + 1; j < list.Count; j++)// 최소값을 탐색한다.
                {
                    if (list[j] < list[min])//초기값 or 최소값이 j보다 크면 j를 최소값에 넣는다.
                    {
                        min = j;
                    }
                }
                if (min != 0)//스왑 알고리즘
                {
                    Swap(i, min);
                }
 
                Console.WriteLine("\n{0}회정렬 후", i);
                for (int a = 0; a < list.Count; a++)
                {
                    Console.Write("{0} ", list[a]);
                }
            }
        }
 
        public void DescendingSelectSort()//내림 차순 정렬
        {
            int i = 0;
            int j = 0;
            int max = 0//최소값의 index를 확인할 아이
 
            for (i = 0; i < list.Count - 1; i++)//전체 훑기 Count-1 : 마지막칸은 자동으로 정렬되고 뒷칸이 없으므로 제외
            {
                max = i;//최소값을 들고 있을 제 3의 손//맨 처음엔 검사하는 첫배열을 들고있음
 
                Console.WriteLine("\n{0}회정렬 전", i);
                for (int a = 0; a < list.Count; a++)
                {
                    Console.Write("{0} ", list[a]);
                }
                for (j = i + 1; j < list.Count; j++)// 최소값을 탐색한다.
                {
                    if (list[j] > list[max])//초기값 or 최소값이 j보다 크면 j를 최소값에 넣는다.
                    {
                        max = j;
                    }
                }
                if (max != 0)//스왑 알고리즘
                {
                    Swap(i, max);
                }
 
                Console.WriteLine("\n{0}회정렬 후", i);
                for (int a = 0; a < list.Count; a++)
                {
                    Console.Write("{0} ", list[a]);
                }
            }
        }
 
        private void Swap(int index1, int index2)
        {
            int temp; //값을 임시로 저장할 temp변수
 
            temp = list[index2];
            list[index2] = list[index1];
            list[index1] = temp;
        }
    }
}
 
cs


'Algorithmus' 카테고리의 다른 글

[Palindrome Number]05-14  (0) 2019.05.14
[TwoSum]05-04  (0) 2019.05.04
[TwoSum]05-03  (0) 2019.05.03
<선택정렬>  (0) 2019.03.29
코딩테스트  (0) 2019.03.27
: