삽입 정렬
Algorithm 2019. 9. 2. 16:19https://gmlwjd9405.github.io/2018/05/06/algorithm-insertion-sort.html
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 = { 8, 5, 6, 2, 4 };
var s = new Solution();
var result = s.solution(arr);
Console.WriteLine(arr[i]);
}
}
}
public class Solution{
public int[] solution(int[] arr) {
int[] answer = { };
int i, j, key;
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 = { 8, 5, 6, 2, 4 };
var s = new Solution();
var result = s.solution(arr);
Console.WriteLine(arr[i]);
}
}
}
public class Solution{
public int[] solution(int[] arr) {
int[] answer = { };
int i, j, key;
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 |