Skip to content

add Insertion-Sort on Java #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Brute Force/Insertion Sort/Code.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import org.algorithm_visualizer.*;
import java.util.Arrays;

class Main {

private static ChartTracer chartTracer = new ChartTracer();

private static LogTracer logTracer = new LogTracer("Console");

private static Integer[] array = (Integer[]) new Randomize.Array1D(15, new Randomize.Integer(1, 20)).create();

public static void main(String[] args) {
int length = array.length;
Layout.setRoot(new VerticalLayout(new Commander[]{chartTracer, logTracer}));
logTracer.printf("original array = %s\n", Arrays.toString(array));
chartTracer.set(array);
Tracer.delay();

for (int i = 1; i < length; i++) {
int j = i - 1;
int temp = array[i];
chartTracer.select(i);
Tracer.delay();
logTracer.printf("insert %s\n",temp);
while (j >= 0 && array[j] > temp) {
array[j + 1] = array[j];
chartTracer.patch(j + 1, array[j + 1]);
Tracer.delay();
chartTracer.depatch(j + 1);
j--;
}
array[j + 1] = temp;
chartTracer.patch(j + 1, temp);
Tracer.delay();
chartTracer.depatch(j + 1);
chartTracer.deselect(i);
}

logTracer.printf("sorted array = %s\n",Arrays.toString(array));
}
}