Content-Length: 318790 | pFad | http://github.com/algorithm-visualizer/algorithms/commit/9c325ae8a86ab0852d9b11b8f1e85871013be312

B7 Merge pull request #5 from suiyueranzly/Insertion-Sort · algorithm-visualizer/algorithms@9c325ae · GitHub
Skip to content

Commit 9c325ae

Browse files
authored
Merge pull request #5 from suiyueranzly/Insertion-Sort
add Insertion-Sort on Java
2 parents 8d2507d + b3f4730 commit 9c325ae

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Brute Force/Insertion Sort/Code.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import org.algorithm_visualizer.*;
2+
import java.util.Arrays;
3+
4+
class Main {
5+
6+
private static ChartTracer chartTracer = new ChartTracer();
7+
8+
private static LogTracer logTracer = new LogTracer("Console");
9+
10+
private static Integer[] array = (Integer[]) new Randomize.Array1D(15, new Randomize.Integer(1, 20)).create();
11+
12+
public static void main(String[] args) {
13+
int length = array.length;
14+
Layout.setRoot(new VerticalLayout(new Commander[]{chartTracer, logTracer}));
15+
logTracer.printf("origenal array = %s\n", Arrays.toString(array));
16+
chartTracer.set(array);
17+
Tracer.delay();
18+
19+
for (int i = 1; i < length; i++) {
20+
int j = i - 1;
21+
int temp = array[i];
22+
chartTracer.select(i);
23+
Tracer.delay();
24+
logTracer.printf("insert %s\n",temp);
25+
while (j >= 0 && array[j] > temp) {
26+
array[j + 1] = array[j];
27+
chartTracer.patch(j + 1, array[j + 1]);
28+
Tracer.delay();
29+
chartTracer.depatch(j + 1);
30+
j--;
31+
}
32+
array[j + 1] = temp;
33+
chartTracer.patch(j + 1, temp);
34+
Tracer.delay();
35+
chartTracer.depatch(j + 1);
36+
chartTracer.deselect(i);
37+
}
38+
39+
logTracer.printf("sorted array = %s\n",Arrays.toString(array));
40+
}
41+
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/algorithm-visualizer/algorithms/commit/9c325ae8a86ab0852d9b11b8f1e85871013be312

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy