-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsert.java
More file actions
41 lines (32 loc) Β· 1.27 KB
/
Insert.java
File metadata and controls
41 lines (32 loc) Β· 1.27 KB
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
package basic.sort.impl;
import basic.sort.Sort;
/*
μ½μ
μ λ ¬
- 2λ²μ§Έ λΆν° μκΈ° μμΉμ μ°Ύμκ°λ κ²μ λ°λ³΅νλ μ λ ¬ λ°©μ
= μ΄λ μμ μ μμΉλ₯Ό μ°Ύμκ°λ€λ κ²μ νμ¬ μμ μ μμΉλ³΄λ€ μμ μ‘΄μ¬νλ μ λ ¬λ μ μ¬μ΄μ μκΈ°λ³΄λ€ μμμ μμ ν°μ λ‘ μ λ ¬λλ μμΉμ μ½μ
νλ λ°©μ
= μ½μ
μ μμ λ³΄λ€ ν° μλ€μ νλμ© λ€λ‘ λ°μ΄μΌν΄μ swapμ΄ λ§μ΄ μΌμ΄λ¨.
- μ΅μ
μ κ²½μ° O(n^2)μ μκ° λ³΅μ‘λλ₯Ό κ°μ§μ§λ§ μ λ ¬μ΄ λμ΄μλ κ²½μ° λΉ λ₯Ό μλ μμ
= κ·Όλ° μ λ ¬μ΄ λμ΄ μμ κ²μ΄λΌλ 보μ₯μ΄ μκΈ°μ ν΅ μ λ ¬μ΄ λ μ’μ.
*/
public class Insert implements Sort {
@Override
public void sort(int[] array) {
for(int i =1;i<array.length;i++){
int now = array[i];
int start = i;
for(int j = i-1 ; j>=0;j--){
if(array[j]>now) start=j;
else break;
}
swapAll(array,start,i);
printAllArray(array,i);
}
}
public void swapAll(int[] array, int start, int end){
int temp = array[end];
for(int i = end; i > start ; i--){
array[i] = array[i-1];
}
array[start] = temp;
}
}