Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6ea9afe

Browse files
suiyueranzly64json
suiyueranzly
authored andcommitted
Comb Sort on Java in Brute Force\Comb Sort (#7)
1 parent12b38cf commit6ea9afe

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+
importorg.algorithm_visualizer.*;
2+
3+
importjava.util.Arrays;
4+
5+
classMain {
6+
7+
privatestaticChartTracerchartTracer =newChartTracer();
8+
9+
privatestaticLogTracerlogTracer =newLogTracer("Console");
10+
11+
privatestaticArray1DTracertracer =newArray1DTracer();
12+
13+
privatestaticInteger[]array = (Integer[])newRandomize.Array1D(15,newRandomize.Integer(1,20)).create();
14+
15+
publicstaticvoidmain(String[]args) {
16+
tracer.set(array);
17+
tracer.chart(chartTracer);
18+
Layout.setRoot(newVerticalLayout(newCommander[]{chartTracer,tracer,logTracer}));
19+
logTracer.printf("original array = %s\n",Arrays.toString(array));
20+
21+
Tracer.delay();
22+
23+
intlength =array.length;
24+
25+
intgap =length;
26+
27+
booleanswapped;
28+
29+
floatshrink =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 (inti =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+
privatestaticvoidswap(intx,inty,Integer[]array) {
59+
inttemp =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)

[8]ページ先頭

©2009-2025 Movatter.jp