forked from sowon-dev/AlgorithmStudy_Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSortPart1.java
More file actions
31 lines (28 loc) ยท 945 Bytes
/
InsertionSortPart1.java
File metadata and controls
31 lines (28 loc) ยท 945 Bytes
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
package hackerrank;
public class InsertionSortPart1 {
static void insertionSort1(int n, int[] arr) {
int sort = arr[arr.length - 1];
//i๋ฅผ for๋ฌธ๋ฐ์์๋ ์ฌ์ฉํด์ผํ๊ธฐ๋๋ฌธ์ ์ ์ญ๋ณ์๋ก ์ ์ธ
int i;
//arr์ ๋์์ ๋๋ฒ์งธ ์์๋ถํฐ ๋ณ์ sort๋ณด๋ค ํฐ ๊ฒฝ์ฐ ์ซ์๋ฅผ ๋ณต์ ํ๋ค.
for (i = arr.length - 2; (i >= 0) && (arr[i] > sort); i--) {
arr[i + 1] = arr[i];
for(int num : arr){
System.out.print(num+" ");
}
System.out.println("");
}
//๋ณ์sort๋ณด๋ค arr ์์๊ฐ ์์ ๊ฒฝ์ฐ ๋ณ์ sort๋ฅผ ๊ทธ ์์ ๋ค์ ์๋ฆฌ์ ๋ฃ๋๋ค.
arr[i + 1] = sort;
for(int num : arr){
System.out.print(num+" ");
}
System.out.println("");
}
public static void main(String[] args) {
insertionSort1(5, new int[]{1,2,4,5,3});
System.out.print(", ans: 1 2 4 5 5\n"
+ "1 2 4 4 5\n"
+ "1 2 3 4 5");
}
}