Skip to content
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

Solve Issue #326 comb sort algorithm #346

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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 src/main/java/com/williamfiset/algorithms/sorting/CombSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.williamfiset.algorithms.sorting;

import java.util.Scanner;

public class CombSort
{

// Function to sort list using Comb Sort
public void combsort(int sortlist[],int gapnumber)
{
int n = sortlist.length;

// initialize gap to length of the list
int gap = n;

// Initialize swapflag
int swapflag = 1;

// Keep running while gap is more than 1 and last
// iteration caused a swap
while (gap != 1 || swapflag == 1)
{
// Find next gap
// 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<n-gap; i++)
{
if (sortlist[i] > sortlist[i+gap])
{
int temp = sortlist[i];
sortlist[i] = sortlist[i+gap];
sortlist[i+gap] = temp;
// Set swapped
swapflag = 1;
}
}
}
}

/**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 combSort = new CombSort();
Scanner scan = new Scanner(System.in);
System.out.println("Enter number of the numbers you want to sort");
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[n];
for (int i=0;i<n;i++)
arr[i]=scan.nextInt();
combSort.combsort(arr,n1);
System.out.println("sorted array");
for (int i=0; i<arr.length; ++i)
System.out.print(arr[i] + " ");

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.williamfiset.algorithms.sorting;

import org.junit.Test;

import java.util.Arrays;
import java.util.Random;

import static com.google.common.truth.Truth.assertThat;

public class CombSortTest {
static Random random = new Random();

CombSort combSort = new CombSort();

@Test
public void randomCombSort_smallNumbers() {
for (int size = 0; size < 1000; size++) {
int[] values = new int[size];
for (int i = 0; i < size; i++) {
values[i] = randInt(1, 50);
}
int[] copy = values.clone();

Arrays.sort(values);
combSort.combsort(copy,2);
assertThat(values).isEqualTo(copy);
}
}

@Test
public void randomRadixSort_largeNumbers() {
for (int size = 0; size < 1000; size++) {
int[] values = new int[size];
for (int i = 0; i < size; i++) {
values[i] = randInt(1, Integer.MAX_VALUE);
}
int[] copy = values.clone();

Arrays.sort(values);
combSort.combsort(copy,2);

assertThat(values).isEqualTo(copy);
}
}

// return a random number between [min, max]
static int randInt(int min, int max) {
return random.nextInt((max - min) + 1) + min;
}
}