삽입 정렬

Algorithm 2019. 9. 2. 16:19
반응형

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

 

[알고리즘] 삽입 정렬(insertion sort)이란 - Heee's Development Blog

Step by step goes a long way.

gmlwjd9405.github.io

https://www.youtube.com/watch?v=16I9Z7bS1iM&t=4s

https://www.youtube.com/watch?v=ROalU379l3U&t=46s

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
using System;
 
namespace Application
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            //삽입정
            int[] arr = { 85624 };
            var s = new Solution();
            var result = s.solution(arr);
            for (int i = 0; i < result.Length; i++) {
                Console.WriteLine(arr[i]);
            }
        }
    }
 
    public class Solution{
        public int[] solution(int[] arr) {
            int[] answer = { };
            int i, j, key;
 
            for (i = 1; i<arr.Length; i++) {
                key = arr[i];
 
                for (j = i - 1; j >= 0 && arr[j] > key; j--) {
                    arr[j + 1= arr[j];
                    
                }
 
                arr[j + 1= key;
            }
 
            
            answer = arr;
            return answer;
 
        }
    }
}
 
 
 

 

 

 

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
using System;
 
namespace Application
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            //삽입정
            int[] arr = { 85624 };
            var s = new Solution();
            var result = s.solution(arr);
            for (int i = 0; i < result.Length; i++) {
                Console.WriteLine(arr[i]);
            }
        }
    }
 
    public class Solution{
        public int[] solution(int[] arr) {
            int[] answer = { };
            int i, j, key;
 
            for (i = 1; i<arr.Length; i++) {
                key = arr[i];
 
                for (j = i - 1; j >= 0; j--) {
                    if (arr[j] > key)
                    {
                        arr[j + 1= arr[j];
                    }
                    else {
                        break;
                    }
                }
 
                arr[j + 1= key;
            }
 
            
            answer = arr;
            return answer;
 
        }
    }
}
 
 
 
반응형

'Algorithm' 카테고리의 다른 글

프로그래머스 | 기지국 설치  (0) 2019.09.06
프로그래머스 | 최소공배수  (0) 2019.09.06
재귀 | n까지의 합  (0) 2019.08.30
선택 정렬  (0) 2019.08.30
이진탐색 | 재귀  (0) 2019.08.30
: