선택 정렬

Algorithm 2019. 8. 30. 14:04
반응형

https://www.youtube.com/watch?v=16I9Z7bS1iM

 

https://www.youtube.com/watch?v=uCUu3fF5Dws

 

https://gmlwjd9405.github.io/2018/05/06/algorithm-selection-sort.html

 

[알고리즘] 선택 정렬(selection sort)이란 - Heee's Development Blog

Step by step goes a long way.

gmlwjd9405.github.io

 

https://dpdpwl.tistory.com/17

 

[Algorithm]선택정렬 예제(selection sort)

안녕하세요~~~ 오늘은 자바로 선택정렬(selection sort) 에 대해 알아 보겠습니다. 버블정렬과 함께 정렬중에 기본인 선택정렬인데요!! 버블정렬과 마찬가지로 느린 정렬에 속합니다. 하지만 정렬에 기본이되기때..

dpdpwl.tistory.com

 

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
using System;
 
namespace Application
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            int[] arr = { 96735 };
            var sol = new Solution();
            var result = sol.solution(arr);
            //선택 정렬
            for (int i = 0; i < result.Length; i++) {
                Console.WriteLine(result[i]);
            }
            
        }
    }
 
    public class Solution {
        public int[] solution(int[] arr) {
            int[] answer = { };
 
            for (int i = 0; i < arr.Length; i++) {
                for (int j = i + 1; j < arr.Length; j++) {
                    
                    if (arr[i] > arr[j])
                    {
                        var temp = arr[j];
                        arr[j] = arr[i];
                        arr[i] = temp;
                    }
                }
            }
 
            answer = arr;
 
            return answer;
        }
 
        public int[] solution2(int[] arr) {
            int[] answer = { };
 
            for (int i = 0; i < arr.Length; i++) {
 
                int startIdx = i;
                int start = arr[i];
 
                for (int j = i+1; j < arr.Length; j++) {
                    if (arr[j] < start) {
                        start = arr[j];
                        startIdx = start;
                    }
                }
 
                int temp = arr[i];
                arr[i] = arr[startIdx];
                arr[startIdx] = temp;
            }
 
            return answer;
        }
    }
}
 
 
 
반응형

'Algorithm' 카테고리의 다른 글

삽입 정렬  (0) 2019.09.02
재귀 | n까지의 합  (0) 2019.08.30
이진탐색 | 재귀  (0) 2019.08.30
소수구하기 | Prime Number | 에라토스테네스의 체  (0) 2019.08.30
LeetCode | Easy | Reverse Integer  (0) 2019.08.29
: