From cd8cdffde7d00c9a59858a8e9a836736f1c2f014 Mon Sep 17 00:00:00 2001 From: suiyueranzly Date: Thu, 14 Mar 2019 10:16:36 +0800 Subject: [PATCH] Comb Sort on Java in Brute Force\Comb Sort --- Brute Force/Comb Sort/Code.java | 69 +++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Brute Force/Comb Sort/Code.java diff --git a/Brute Force/Comb Sort/Code.java b/Brute Force/Comb Sort/Code.java new file mode 100644 index 00000000..a3635aa3 --- /dev/null +++ b/Brute Force/Comb Sort/Code.java @@ -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); + } + +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy