Skip to content

Commit 4bbf5d0

Browse files
committed
issue #27 Insertion Sort Part 1 w/ Explanation
1 parent 7579800 commit 4bbf5d0

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package hackerrank;
2+
3+
public class InsertionSortPart1 {
4+
static void insertionSort1(int n, int[] arr) {
5+
int sort = arr[arr.length - 1];
6+
7+
//i๋ฅผ for๋ฌธ๋ฐ–์—์„œ๋„ ์‚ฌ์šฉํ•ด์•ผํ•˜๊ธฐ๋•Œ๋ฌธ์— ์ „์—ญ๋ณ€์ˆ˜๋กœ ์„ ์–ธ
8+
int i;
9+
//arr์˜ ๋์—์„œ ๋‘๋ฒˆ์งธ ์š”์†Œ๋ถ€ํ„ฐ ๋ณ€์ˆ˜ sort๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ ์ˆซ์ž๋ฅผ ๋ณต์ œํ•œ๋‹ค.
10+
for (i = arr.length - 2; (i >= 0) && (arr[i] > sort); i--) {
11+
arr[i + 1] = arr[i];
12+
for(int num : arr){
13+
System.out.print(num+" ");
14+
}
15+
System.out.println("");
16+
}
17+
//๋ณ€์ˆ˜sort๋ณด๋‹ค arr ์š”์†Œ๊ฐ€ ์ž‘์€ ๊ฒฝ์šฐ ๋ณ€์ˆ˜ sort๋ฅผ ๊ทธ ์š”์†Œ ๋‹ค์Œ ์ž๋ฆฌ์— ๋„ฃ๋Š”๋‹ค.
18+
arr[i + 1] = sort;
19+
for(int num : arr){
20+
System.out.print(num+" ");
21+
}
22+
System.out.println("");
23+
}
24+
25+
public static void main(String[] args) {
26+
insertionSort1(5, new int[]{1,2,4,5,3});
27+
System.out.print(", ans: 1 2 4 5 5\n"
28+
+ "1 2 4 4 5\n"
29+
+ "1 2 3 4 5");
30+
}
31+
}

0 commit comments

Comments
ย (0)