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

Added CombSortTest.java #825

Closed
wants to merge 2 commits into from
Closed
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
61 changes: 61 additions & 0 deletions src/main/java/com/sorts/CombSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.sorts;

import com.types.Sort;

/**
* Comb Sort algorithm implementation
* <p>
* Best-case performance O(n * log(n))
* Worst-case performance O(n ^ 2)
* Worst-case space complexity O(1)
* <p>
* Comb sort improves on bubble sort.
*
* @author Sandeep Roy (https://github.com/sandeeproy99)
* @author Podshivalov Nikita (https://github.com/nikitap492)
*
*/
class CombSort<T> implements Sort<T> {

// To find gap between elements
private int nextGap(int gap) {
// Shrink gap by Shrink factor
gap = (gap * 10) / 13;
return (gap < 1) ? 1 : gap;
}

/**
* Function to sort arr[] using Comb
*
* @param arr - an array should be sorted
* @return sorted array
*/
@Override
public <T extends Comparable<T>> T[] sort(T arr[]) {
int size = arr.length;

// initialize gap
int gap = size;

// 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) {
// Find next gap
gap = nextGap(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 < size - gap; i++) {
if (SortUtils.less(arr[i + gap], arr[i])) {
// Swap arr[i] and arr[i+gap]
swapped = SortUtils.swap(arr, i, i + gap);
}
}
}
return arr;
}
}
77 changes: 77 additions & 0 deletions src/test/java/com/sorts/CombSortTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.sorts;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;

class CombSortTest {

CombSort combSort = new CombSort();

@Test
void testIntegerSort(){

Integer array1[] = { 9, 5, 4, 6, 0, 100, 91, 40, 140, 500, 4, -1, -19};
Integer array2[] = { 9, 5, 4, 6, 0, 100, 91, 40, 140, 500, 4, -1, -19};

//using Java's tested sort algorithm to sort our unsorted array
Arrays.sort(array1);

//sort our unsorted array using our new CombSort algorithm
combSort.sort(array2);

//compare the two results, our CombSort vs. Java's sort
Assertions.assertArrayEquals(array1,array2);
}

@Test
void testStringSort(){

String array1[] = { "Ten" , "Nine", "Eight", "Seven", "Six", "Five", "Four", "One", "Zero" };
String array2[] = { "Ten" , "Nine", "Eight", "Seven", "Six", "Five", "Four", "One", "Zero" };

//using Java's tested sort algorithm to sort our unsorted array
Arrays.sort(array1);

//sort our unsorted array using our new CombSort algorithm
combSort.sort(array2);

//compare the two results, our CombSort vs. Java's sort
Assertions.assertArrayEquals(array1,array2);
}

@Test
void testDoubleSort(){

Double array1[] = { 90.0, -10.0, 0.0, -1.0, -23.98, 10.76, 20.92 };
Double array2[] = { 90.0, -10.0, 0.0, -1.0, -23.98, 10.76, 20.92 };

//using Java's tested sort algorithm to sort our unsorted array
Arrays.sort(array1);

//sort our unsorted array using our new CombSort algorithm
combSort.sort(array2);

//compare the two results, our CombSort vs. Java's sort
Assertions.assertArrayEquals(array1,array2);
}

@Test
void testCharacterSort(){

Character array1[] = { 'Z', 'A', 'B', 'z', 'b', 'c' };
Character array2[] = { 'Z', 'A', 'B', 'z', 'b', 'c' };

//using Java's tested sort algorithm to sort our unsorted array
Arrays.sort(array1);

//sort our unsorted array using our new CombSort algorithm
combSort.sort(array2);

//compare the two results, our CombSort vs. Java's sort
Assertions.assertArrayEquals(array1,array2);
}
}