|
| 1 | +importorg.algorithm_visualizer.*; |
| 2 | +importjava.util.Arrays; |
| 3 | + |
| 4 | +classMain { |
| 5 | + |
| 6 | +privatestaticChartTracerchartTracer =newChartTracer(); |
| 7 | + |
| 8 | +privatestaticLogTracerlogTracer =newLogTracer("Console"); |
| 9 | + |
| 10 | +privatestaticInteger[]array = (Integer[])newRandomize.Array1D(15,newRandomize.Integer(1,20)).create(); |
| 11 | + |
| 12 | +publicstaticvoidmain(String[]args) { |
| 13 | +intlength =array.length; |
| 14 | +Layout.setRoot(newVerticalLayout(newCommander[]{chartTracer,logTracer})); |
| 15 | +logTracer.printf("original array = %s\n",Arrays.toString(array)); |
| 16 | +chartTracer.set(array); |
| 17 | +Tracer.delay(); |
| 18 | + |
| 19 | +for (inti =1;i <length;i++) { |
| 20 | +intj =i -1; |
| 21 | +inttemp =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 | +} |