Skip to content

Commit 6ea9afe

Browse files
suiyueranzly64json
authored andcommitted
Comb Sort on Java in Brute Force\Comb Sort (#7)
1 parent 12b38cf commit 6ea9afe

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

Brute Force/Comb Sort/Code.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import org.algorithm_visualizer.*;
2+
3+
import java.util.Arrays;
4+
5+
class Main {
6+
7+
private static ChartTracer chartTracer = new ChartTracer();
8+
9+
private static LogTracer logTracer = new LogTracer("Console");
10+
11+
private static Array1DTracer tracer = new Array1DTracer();
12+
13+
private static Integer[] array = (Integer[]) new Randomize.Array1D(15, new Randomize.Integer(1, 20)).create();
14+
15+
public static void main(String[] args) {
16+
tracer.set(array);
17+
tracer.chart(chartTracer);
18+
Layout.setRoot(new VerticalLayout(new Commander[]{chartTracer, tracer, logTracer}));
19+
logTracer.printf("original array = %s\n", Arrays.toString(array));
20+
21+
Tracer.delay();
22+
23+
int length = array.length;
24+
25+
int gap = length;
26+
27+
boolean swapped;
28+
29+
float shrink = 1.3f;
30+
31+
do {
32+
swapped = false;
33+
34+
gap = (int) Math.floor(gap / shrink);
35+
36+
if(gap < 1){
37+
gap = 1;
38+
}
39+
40+
for (int i = 0; i + gap < length; i++) {
41+
tracer.select(i);
42+
tracer.select(i + gap);
43+
Tracer.delay();
44+
if (array[i] > array[i + gap]) {
45+
swap(i, i + gap, array);
46+
swapped = true;
47+
}
48+
tracer.deselect(i);
49+
tracer.deselect(i + gap);
50+
}
51+
52+
} while (gap != 1 || swapped);
53+
54+
55+
logTracer.printf("sorted array = %s\n", Arrays.toString(array));
56+
}
57+
58+
private static void swap(int x, int y, Integer[] array) {
59+
int temp = array[x];
60+
array[x] = array[y];
61+
array[y] = temp;
62+
tracer.patch(x, array[x]);
63+
tracer.patch(y, array[y]);
64+
Tracer.delay();
65+
tracer.depatch(x);
66+
tracer.depatch(y);
67+
}
68+
69+
}

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