<선택정렬>

Algorithmus 2019. 3. 29. 00:42
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190328
{
    class App
    {
        public List<int> list = new List<int> { 12605 };
 
        public App()
        {
 
        }
 
        public void Run()
        {
            Console.WriteLine("실행");
            this.SelectSort();
        }
 
        public void SelectSort()
        {
            int i = 0;
            int j = 0;
            int min = 0//최소값의 index를 확인할 아이
            int temp; //값을 임시로 저장할 temp변수
            
            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)//스왑 알고리즘
                {
                    temp = list[min];
                    list[min] = list[i];
                    list[i] = temp;
                }
                
                Console.WriteLine("\n{0}회정렬 후", i);
                for (int a = 0; a < list.Count; a++)
                {
                    Console.Write("{0} ", list[a]);
                }
            }
        }
    }
}
cs


'Algorithmus' 카테고리의 다른 글

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