From 14a6994a580978ce54fa91725ad9d4a792b13f9e Mon Sep 17 00:00:00 2001 From: SHEELINUNI Date: Sun, 23 Oct 2022 11:45:56 +1100 Subject: [PATCH 1/6] Do hamiltonian cycle checking --- .../graphtheory/HamiltonianCycle.java | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java diff --git a/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java b/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java new file mode 100644 index 000000000..b21f30d73 --- /dev/null +++ b/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java @@ -0,0 +1,108 @@ +package com.williamfiset.algorithms.graphtheory; + +import java.util.Scanner; +import java.util.Arrays; + +public class HamiltonianCycle { + /**elements used to store path and the graph **/ + private int V, pathCount; + private int[] path; + private int[][] graph; + + /** Function to find cycle + * Take the graph as input + * And will print "No solution" if the graph has no HamiltionianCycle + * Function solve will do actual find paths job + * **/ + public void findHamiltonianCycle(int[][] g) + { + V = g.length; + path = new int[V]; + Arrays.fill(path, -1); + graph = g; + try + { + path[0] = 0; + pathCount = 1; + solve(0); + System.out.println("No solution"); + } + catch (Exception e) + { + System.out.println(e.getMessage()); + display(); + } + } + + /** function to find paths recursively + *Take int as input + *Will add coordinators into the path list. + * **/ + public void solve(int vertex) throws Exception + { + /** solution **/ + if (graph[vertex][0] == 1 && pathCount == V) + throw new Exception("Solution found"); + /** all vertices selected but last vertex not linked to 0 **/ + if (pathCount == V) + return; + for (int v = 0; v < V; v++) + { + /** if connected **/ + if (graph[vertex][v] == 1) + { + /** add to path **/ + path[pathCount++] = v; + /** remove connection **/ + graph[vertex][v] = 0; + graph[v][vertex] = 0; + /** if vertex not already selected solve recursively **/ + if (!isPresent(v)) + solve(v); + /** restore connection **/ + graph[vertex][v] = 1; + graph[v][vertex] = 1; + /** remove path **/ + path[--pathCount] = -1; + } + } + } + + /** function to check if path is already selected **/ + public boolean isPresent(int v) + { + for (int i = 0; i < pathCount - 1; i++) + if (path[i] == v) + return true; + return false; + } + + /** display solution **/ + public void display() + { + System.out.print("\nPath : "); + for (int i = 0; i <= V; i++) + System.out.print(path[i % V] + " "); + System.out.println(); + } + + /** Main function **/ + public static void main(String[] args) + { + Scanner scan = new Scanner(System.in); + /** Make an object of HamiltonianCycle class **/ + HamiltonianCycle hc = new HamiltonianCycle(); + /** Accept number of vertices **/ + System.out.println("Enter number of vertices"); + int V = scan.nextInt(); + /** get graph **/ + System.out.println("Enter adjacency matrix"); + int[][] graph = new int[V][V]; + for (int i = 0; i < V; i++) + for (int j = 0; j < V; j++) + graph[i][j] = scan.nextInt(); + hc.findHamiltonianCycle(graph); + scan.close(); + } + } + From 55cde931d129dfbe3806be16f930e5802cc5f76d Mon Sep 17 00:00:00 2001 From: SHEELINUNI Date: Sun, 23 Oct 2022 13:21:28 +1100 Subject: [PATCH 2/6] Test for HamiltonianCycle --- .../graphtheory/HamiltonianCycle.java | 15 ++- .../graphtheory/HamiltonianCycleTest.java | 124 ++++++++++++++++++ 2 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycleTest.java diff --git a/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java b/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java index b21f30d73..bb25f2476 100644 --- a/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java +++ b/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java @@ -1,5 +1,6 @@ package com.williamfiset.algorithms.graphtheory; +import java.util.List; import java.util.Scanner; import java.util.Arrays; @@ -9,6 +10,13 @@ public class HamiltonianCycle { private int[] path; private int[][] graph; + public void setGraph(int[][] graph1){ + this.graph=graph1; + } + + public int[] getPath(){ + return this.path; + } /** Function to find cycle * Take the graph as input * And will print "No solution" if the graph has no HamiltionianCycle @@ -16,8 +24,9 @@ public class HamiltonianCycle { * **/ public void findHamiltonianCycle(int[][] g) { + if(g==null)throw new IllegalArgumentException("Graph cannot be null."); V = g.length; - path = new int[V]; + path = new int[V+1]; Arrays.fill(path, -1); graph = g; try @@ -25,10 +34,11 @@ public void findHamiltonianCycle(int[][] g) path[0] = 0; pathCount = 1; solve(0); - System.out.println("No solution"); + System.out.print("No solution"); } catch (Exception e) { + path[V]=path[0]; System.out.println(e.getMessage()); display(); } @@ -66,6 +76,7 @@ public void solve(int vertex) throws Exception path[--pathCount] = -1; } } + } /** function to check if path is already selected **/ diff --git a/src/test/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycleTest.java b/src/test/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycleTest.java new file mode 100644 index 000000000..d8baa7234 --- /dev/null +++ b/src/test/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycleTest.java @@ -0,0 +1,124 @@ +package com.williamfiset.algorithms.graphtheory; + +import org.junit.*; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.*; + +import static com.google.common.truth.Truth.assertThat; + +public class HamiltonianCycleTest { + + HamiltonianCycle solver; + + @Before + public void setUp() { + solver = null; + } + + // Initialize graph with 'n' nodes. + public static int[][] initializeEmptyGraph() { + int[][] graph = null; + return graph; + } + + // Add directed edge to graph. + public static void addDirectedEdge(int[][] graph, int a, int b) { + graph[a][b]=1; + } + + //test empty + @Test(expected = IllegalArgumentException.class) + public void testEmptyGraph() { + int[][] graph = initializeEmptyGraph(); + HamiltonianCycle solver; + solver = new HamiltonianCycle(); + solver.setGraph(graph); + solver.findHamiltonianCycle(graph); + } + + //valid test 1 + @Test + public void validHC1() { + int[][] graph = new int[5][5]; + int[] result= new int[6]; + result[0]=0;result[1]=1;result[2]=2;result[3]=4;result[4]=3;result[5]=0; + /** + * the example graph + * (0)--(1)--(2) + * | / \ | + * | / \ | + * | / \ | + * (3)-------(4) + * + * the 2D should be + * 0 1 0 1 0 + * 1 0 1 1 1 + * 0 1 0 0 1 + * 1 1 0 0 1 + * 0 1 1 1 0 + * + */ + graph [0][0]=0;graph [0][1]=1;graph [0][2]=0;graph [0][3]=1;graph [0][4]=0; + graph [1][0]=1;graph [1][1]=0;graph [1][2]=1;graph [1][3]=1;graph [1][4]=1; + graph [2][0]=0;graph [2][1]=1;graph [2][2]=0;graph [2][3]=0;graph [2][4]=1; + graph [3][0]=1;graph [3][1]=1;graph [3][2]=0;graph [3][3]=0;graph [3][4]=1; + graph [4][0]=0;graph [4][1]=1;graph [4][2]=1;graph [4][3]=1;graph [4][4]=0; + HamiltonianCycle solver; + solver = new HamiltonianCycle(); + solver.setGraph(graph); + solver.findHamiltonianCycle(graph); + assertThat(solver.getPath()).isEqualTo(result); + } + + //invalid test 1, generally same as valid 1, but change a little and make it invalid + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + private final PrintStream originalErr = System.err; + + @Before + public void setUpStreams() { + System.setOut(new PrintStream(outContent)); + System.setErr(new PrintStream(errContent)); + } + + @After + public void restoreStreams() { + System.setOut(originalOut); + System.setErr(originalErr); + } + @Test + public void invalidHC1() { + int[][] graph = new int[5][5]; + int[] invalidresult= new int[6]; + Arrays.fill(invalidresult, -1); + /** + * the example graph + * (0)--(1)--(2) + * | / \ | + * | / \ | + * | / \ | + * (3) (4) + * + * the 2D should be + * 0 1 0 1 0 + * 1 0 1 1 1 + * 0 1 0 0 1 + * 1 1 0 0 0 + * 0 1 1 0 0 + * + */ + graph [0][0]=0;graph [0][1]=1;graph [0][2]=0;graph [0][3]=1;graph [0][4]=0; + graph [1][0]=1;graph [1][1]=0;graph [1][2]=1;graph [1][3]=1;graph [1][4]=1; + graph [2][0]=0;graph [2][1]=1;graph [2][2]=0;graph [2][3]=0;graph [2][4]=1; + graph [3][0]=1;graph [3][1]=1;graph [3][2]=0;graph [3][3]=0;graph [3][4]=0; + graph [4][0]=0;graph [4][1]=1;graph [4][2]=1;graph [4][3]=0;graph [4][4]=0; + HamiltonianCycle solver; + solver = new HamiltonianCycle(); + solver.setGraph(graph); + solver.findHamiltonianCycle(graph); + Assert.assertEquals("No solution",outContent.toString()); + } +} From 928be43729d02cc77422b7178d0a6fddc3bd0e58 Mon Sep 17 00:00:00 2001 From: SHEELINUNI Date: Mon, 24 Oct 2022 16:36:00 +1100 Subject: [PATCH 3/6] Finish CombSort --- .../algorithms/sorting/CombSort.java | 74 +++++++++++++++++++ .../algorithms/sorting/CombSortTest.java | 61 +++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 src/main/java/com/williamfiset/algorithms/sorting/CombSort.java create mode 100644 src/test/java/com/williamfiset/algorithms/sorting/CombSortTest.java diff --git a/src/main/java/com/williamfiset/algorithms/sorting/CombSort.java b/src/main/java/com/williamfiset/algorithms/sorting/CombSort.java new file mode 100644 index 000000000..ab04e1b27 --- /dev/null +++ b/src/main/java/com/williamfiset/algorithms/sorting/CombSort.java @@ -0,0 +1,74 @@ +package com.williamfiset.algorithms.sorting; + +import java.util.Scanner; + +class CombSort +{ + // To find gap between elements + int getNextGap(int gap) + { + // Shrink gap by Shrink factor + gap = (gap*10)/13; + if (gap < 1) + return 1; + return gap; + } + + // Function to sort arr[] using Comb Sort + void sort(int arr[]) + { + int n = arr.length; + + // initialize gap + int gap = n; + + // Initialize swapped as true to make sure that + // loop runs + boolean swapped = true; + + // Keep running while gap is more than 1 and last + // iteration caused a swap + while (gap != 1 || swapped == true) + { + // Find next gap + gap = getNextGap(gap); + + // Initialize swapped as false so that we can + // check if swap happened or not + swapped = false; + + // Compare all elements with current gap + for (int i=0; i arr[i+gap]) + { + // Swap arr[i] and arr[i+gap] + int temp = arr[i]; + arr[i] = arr[i+gap]; + arr[i+gap] = temp; + + // Set swapped + swapped = true; + } + } + } + } + + // Driver method + public static void main(String args[]) + { + CombSort ob = new CombSort(); + Scanner scan = new Scanner(System.in); + System.out.println("Enter number of the numbers you want to sort"); + int V = scan.nextInt(); + System.out.println("Enter the numbers:"); + int[] arr = new int[V]; + for (int i=0;i Date: Mon, 24 Oct 2022 16:38:32 +1100 Subject: [PATCH 4/6] Update combsort Test --- .../algorithms/sorting/CombSortTest.java | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/src/test/java/com/williamfiset/algorithms/sorting/CombSortTest.java b/src/test/java/com/williamfiset/algorithms/sorting/CombSortTest.java index 78c09979c..8fb6a7aaf 100644 --- a/src/test/java/com/williamfiset/algorithms/sorting/CombSortTest.java +++ b/src/test/java/com/williamfiset/algorithms/sorting/CombSortTest.java @@ -10,20 +10,10 @@ public class CombSortTest { static Random random = new Random(); - @Test - public void testGetMax() { - int[] array = {5, 7, 1, 13, 1013, 287, 912}; - assertThat(RadixSort.getMax(array)).isEqualTo(1013); - } - - @Test - public void testCalculateNumberOfDigits() { - assertThat(RadixSort.calculateNumberOfDigits(1089)).isEqualTo(4); - assertThat(RadixSort.calculateNumberOfDigits(19)).isEqualTo(2); - } + CombSort ob = new CombSort(); @Test - public void randomRadixSort_smallNumbers() { + public void randomCombSort_smallNumbers() { for (int size = 0; size < 1000; size++) { int[] values = new int[size]; for (int i = 0; i < size; i++) { @@ -32,8 +22,7 @@ public void randomRadixSort_smallNumbers() { int[] copy = values.clone(); Arrays.sort(values); - RadixSort.radixSort(copy); - + ob.sort(copy); assertThat(values).isEqualTo(copy); } } @@ -48,7 +37,7 @@ public void randomRadixSort_largeNumbers() { int[] copy = values.clone(); Arrays.sort(values); - RadixSort.radixSort(copy); + ob.sort(copy); assertThat(values).isEqualTo(copy); } From 3ff7a2ae7ef44a2dd909999687c1e11effbca9d1 Mon Sep 17 00:00:00 2001 From: SHEELINUNI Date: Thu, 27 Oct 2022 10:42:04 +1100 Subject: [PATCH 5/6] Delete HamiltonianCycle.java and its test, make comb sort be a single branch and more clear for a single Pull Request --- .../graphtheory/HamiltonianCycle.java | 119 ----------------- .../graphtheory/HamiltonianCycleTest.java | 124 ------------------ 2 files changed, 243 deletions(-) delete mode 100644 src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java delete mode 100644 src/test/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycleTest.java diff --git a/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java b/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java deleted file mode 100644 index bb25f2476..000000000 --- a/src/main/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycle.java +++ /dev/null @@ -1,119 +0,0 @@ -package com.williamfiset.algorithms.graphtheory; - -import java.util.List; -import java.util.Scanner; -import java.util.Arrays; - -public class HamiltonianCycle { - /**elements used to store path and the graph **/ - private int V, pathCount; - private int[] path; - private int[][] graph; - - public void setGraph(int[][] graph1){ - this.graph=graph1; - } - - public int[] getPath(){ - return this.path; - } - /** Function to find cycle - * Take the graph as input - * And will print "No solution" if the graph has no HamiltionianCycle - * Function solve will do actual find paths job - * **/ - public void findHamiltonianCycle(int[][] g) - { - if(g==null)throw new IllegalArgumentException("Graph cannot be null."); - V = g.length; - path = new int[V+1]; - Arrays.fill(path, -1); - graph = g; - try - { - path[0] = 0; - pathCount = 1; - solve(0); - System.out.print("No solution"); - } - catch (Exception e) - { - path[V]=path[0]; - System.out.println(e.getMessage()); - display(); - } - } - - /** function to find paths recursively - *Take int as input - *Will add coordinators into the path list. - * **/ - public void solve(int vertex) throws Exception - { - /** solution **/ - if (graph[vertex][0] == 1 && pathCount == V) - throw new Exception("Solution found"); - /** all vertices selected but last vertex not linked to 0 **/ - if (pathCount == V) - return; - for (int v = 0; v < V; v++) - { - /** if connected **/ - if (graph[vertex][v] == 1) - { - /** add to path **/ - path[pathCount++] = v; - /** remove connection **/ - graph[vertex][v] = 0; - graph[v][vertex] = 0; - /** if vertex not already selected solve recursively **/ - if (!isPresent(v)) - solve(v); - /** restore connection **/ - graph[vertex][v] = 1; - graph[v][vertex] = 1; - /** remove path **/ - path[--pathCount] = -1; - } - } - - } - - /** function to check if path is already selected **/ - public boolean isPresent(int v) - { - for (int i = 0; i < pathCount - 1; i++) - if (path[i] == v) - return true; - return false; - } - - /** display solution **/ - public void display() - { - System.out.print("\nPath : "); - for (int i = 0; i <= V; i++) - System.out.print(path[i % V] + " "); - System.out.println(); - } - - /** Main function **/ - public static void main(String[] args) - { - Scanner scan = new Scanner(System.in); - /** Make an object of HamiltonianCycle class **/ - HamiltonianCycle hc = new HamiltonianCycle(); - /** Accept number of vertices **/ - System.out.println("Enter number of vertices"); - int V = scan.nextInt(); - /** get graph **/ - System.out.println("Enter adjacency matrix"); - int[][] graph = new int[V][V]; - for (int i = 0; i < V; i++) - for (int j = 0; j < V; j++) - graph[i][j] = scan.nextInt(); - hc.findHamiltonianCycle(graph); - scan.close(); - } - } - diff --git a/src/test/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycleTest.java b/src/test/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycleTest.java deleted file mode 100644 index d8baa7234..000000000 --- a/src/test/java/com/williamfiset/algorithms/graphtheory/HamiltonianCycleTest.java +++ /dev/null @@ -1,124 +0,0 @@ -package com.williamfiset.algorithms.graphtheory; - -import org.junit.*; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.*; - -import static com.google.common.truth.Truth.assertThat; - -public class HamiltonianCycleTest { - - HamiltonianCycle solver; - - @Before - public void setUp() { - solver = null; - } - - // Initialize graph with 'n' nodes. - public static int[][] initializeEmptyGraph() { - int[][] graph = null; - return graph; - } - - // Add directed edge to graph. - public static void addDirectedEdge(int[][] graph, int a, int b) { - graph[a][b]=1; - } - - //test empty - @Test(expected = IllegalArgumentException.class) - public void testEmptyGraph() { - int[][] graph = initializeEmptyGraph(); - HamiltonianCycle solver; - solver = new HamiltonianCycle(); - solver.setGraph(graph); - solver.findHamiltonianCycle(graph); - } - - //valid test 1 - @Test - public void validHC1() { - int[][] graph = new int[5][5]; - int[] result= new int[6]; - result[0]=0;result[1]=1;result[2]=2;result[3]=4;result[4]=3;result[5]=0; - /** - * the example graph - * (0)--(1)--(2) - * | / \ | - * | / \ | - * | / \ | - * (3)-------(4) - * - * the 2D should be - * 0 1 0 1 0 - * 1 0 1 1 1 - * 0 1 0 0 1 - * 1 1 0 0 1 - * 0 1 1 1 0 - * - */ - graph [0][0]=0;graph [0][1]=1;graph [0][2]=0;graph [0][3]=1;graph [0][4]=0; - graph [1][0]=1;graph [1][1]=0;graph [1][2]=1;graph [1][3]=1;graph [1][4]=1; - graph [2][0]=0;graph [2][1]=1;graph [2][2]=0;graph [2][3]=0;graph [2][4]=1; - graph [3][0]=1;graph [3][1]=1;graph [3][2]=0;graph [3][3]=0;graph [3][4]=1; - graph [4][0]=0;graph [4][1]=1;graph [4][2]=1;graph [4][3]=1;graph [4][4]=0; - HamiltonianCycle solver; - solver = new HamiltonianCycle(); - solver.setGraph(graph); - solver.findHamiltonianCycle(graph); - assertThat(solver.getPath()).isEqualTo(result); - } - - //invalid test 1, generally same as valid 1, but change a little and make it invalid - private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); - private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); - private final PrintStream originalOut = System.out; - private final PrintStream originalErr = System.err; - - @Before - public void setUpStreams() { - System.setOut(new PrintStream(outContent)); - System.setErr(new PrintStream(errContent)); - } - - @After - public void restoreStreams() { - System.setOut(originalOut); - System.setErr(originalErr); - } - @Test - public void invalidHC1() { - int[][] graph = new int[5][5]; - int[] invalidresult= new int[6]; - Arrays.fill(invalidresult, -1); - /** - * the example graph - * (0)--(1)--(2) - * | / \ | - * | / \ | - * | / \ | - * (3) (4) - * - * the 2D should be - * 0 1 0 1 0 - * 1 0 1 1 1 - * 0 1 0 0 1 - * 1 1 0 0 0 - * 0 1 1 0 0 - * - */ - graph [0][0]=0;graph [0][1]=1;graph [0][2]=0;graph [0][3]=1;graph [0][4]=0; - graph [1][0]=1;graph [1][1]=0;graph [1][2]=1;graph [1][3]=1;graph [1][4]=1; - graph [2][0]=0;graph [2][1]=1;graph [2][2]=0;graph [2][3]=0;graph [2][4]=1; - graph [3][0]=1;graph [3][1]=1;graph [3][2]=0;graph [3][3]=0;graph [3][4]=0; - graph [4][0]=0;graph [4][1]=1;graph [4][2]=1;graph [4][3]=0;graph [4][4]=0; - HamiltonianCycle solver; - solver = new HamiltonianCycle(); - solver.setGraph(graph); - solver.findHamiltonianCycle(graph); - Assert.assertEquals("No solution",outContent.toString()); - } -} From 7d0530095a6b1987d060f2ee8a652dd37a0c6ad4 Mon Sep 17 00:00:00 2001 From: SHEELINUNI Date: Thu, 27 Oct 2022 11:00:24 +1100 Subject: [PATCH 6/6] Update comb sort function and its test --- .../algorithms/sorting/CombSort.java | 67 +++++++++---------- .../algorithms/sorting/CombSortTest.java | 6 +- 2 files changed, 34 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/williamfiset/algorithms/sorting/CombSort.java b/src/main/java/com/williamfiset/algorithms/sorting/CombSort.java index ab04e1b27..2fc2e91f1 100644 --- a/src/main/java/com/williamfiset/algorithms/sorting/CombSort.java +++ b/src/main/java/com/williamfiset/algorithms/sorting/CombSort.java @@ -2,70 +2,65 @@ import java.util.Scanner; -class CombSort +public class CombSort { - // To find gap between elements - int getNextGap(int gap) - { - // Shrink gap by Shrink factor - gap = (gap*10)/13; - if (gap < 1) - return 1; - return gap; - } - // Function to sort arr[] using Comb Sort - void sort(int arr[]) + // Function to sort list using Comb Sort + public void combsort(int sortlist[],int gapnumber) { - int n = arr.length; + int n = sortlist.length; - // initialize gap + // initialize gap to length of the list int gap = n; - // Initialize swapped as true to make sure that - // loop runs - boolean swapped = true; + // Initialize swapflag + int swapflag = 1; // Keep running while gap is more than 1 and last // iteration caused a swap - while (gap != 1 || swapped == true) + while (gap != 1 || swapflag == 1) { // Find next gap - gap = getNextGap(gap); - - // Initialize swapped as false so that we can - // check if swap happened or not - swapped = false; + // To find gap between elements + gap = (gap)/gapnumber; + if (gap < 1){ + gap=1; + } + System.out.println(gap); + // Initialize swapflag + swapflag = 0; // Compare all elements with current gap for (int i=0; i arr[i+gap]) + if (sortlist[i] > sortlist[i+gap]) { - // Swap arr[i] and arr[i+gap] - int temp = arr[i]; - arr[i] = arr[i+gap]; - arr[i+gap] = temp; - + int temp = sortlist[i]; + sortlist[i] = sortlist[i+gap]; + sortlist[i+gap] = temp; // Set swapped - swapped = true; + swapflag = 1; } } } } - // Driver method + /**main method, allow users enter number the want to sort, and then scan the number into a int list, + * the program will use comb sort to sort the int list and print it out for users + */ public static void main(String args[]) { - CombSort ob = new CombSort(); + CombSort combSort = new CombSort(); Scanner scan = new Scanner(System.in); System.out.println("Enter number of the numbers you want to sort"); - int V = scan.nextInt(); + int n = scan.nextInt(); + System.out.println("Enter number of gap you want to use in the sort"); + int n1 = scan.nextInt(); System.out.println("Enter the numbers:"); - int[] arr = new int[V]; - for (int i=0;i