Skip to content

Commit b3f4730

Browse files
author
suiyueranzly
committed
Insertion-Sort on Java in Brute Force\Insertion Sort
1 parent 8d2507d commit b3f4730

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("original 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy