Skip to content

add Comb Sort on Java #7

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
Jun 3, 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
69 changes: 69 additions & 0 deletions Brute Force/Comb Sort/Code.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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 Array1DTracer tracer = new Array1DTracer();

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

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

Tracer.delay();

int length = array.length;

int gap = length;

boolean swapped;

float shrink = 1.3f;

do {
swapped = false;

gap = (int) Math.floor(gap / shrink);

if(gap < 1){
gap = 1;
}

for (int i = 0; i + gap < length; i++) {
tracer.select(i);
tracer.select(i + gap);
Tracer.delay();
if (array[i] > array[i + gap]) {
swap(i, i + gap, array);
swapped = true;
}
tracer.deselect(i);
tracer.deselect(i + gap);
}

} while (gap != 1 || swapped);


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

private static void swap(int x, int y, Integer[] array) {
int temp = array[x];
array[x] = array[y];
array[y] = temp;
tracer.patch(x, array[x]);
tracer.patch(y, array[y]);
Tracer.delay();
tracer.depatch(x);
tracer.depatch(y);
}

}