|
| 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 | +} |