Skip to content

Commit c41a761

Browse files
committed
Insertion Sort Algorithm Code
1 parent 80acff7 commit c41a761

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

InsertionSorter.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ch03;
2+
3+
import java.util.Arrays;
4+
5+
public class InsertionSorter {
6+
7+
public static void main(String[] args) {
8+
int[] data = {7,3,6,8,2};
9+
new InsertionSorter().sort(data);
10+
System.out.println(Arrays.toString(data));
11+
}
12+
13+
public void sort(int[] data) {
14+
for (int i =0; i < data.length; i++) {
15+
int current = data[i];
16+
int j = i-1;
17+
while (j >=0 && data[j] >= current) {
18+
data[j+1] = data[j];
19+
j--;
20+
}
21+
data[j+1] = current;
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)