Skip to content

Fixing 'Generics' issue in Sort #1180

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
Dec 8, 2019
Merged
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
4 changes: 2 additions & 2 deletions src/main/java/com/sorts/BubbleSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.types.Sort;

public class BubbleSort<T> implements Sort<T> {
public class BubbleSort<T extends Comparable<T>> implements Sort<T> {
/**
* This method implements the Generic Bubble Sort
*
Expand All @@ -11,7 +11,7 @@ public class BubbleSort<T> implements Sort<T> {
**/

@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
public T[] sort(T[] array) {
int last = array.length;
//Sorting
boolean swap;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/types/DataStructure.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.Iterator;

/**
* This interface is to define bacis functionality expected out of any implementation class
* This interface is to define basic functionality expected out of any implementation class
* Since this is a data structure it should have the flexibility to contain any kind of object hence it has been made generic
* Any implementation class need not to be thread safe or it could be depending on the implementation class how does it want to behave.
*
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/types/Sort.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.types;

@FunctionalInterface
public interface Sort<T> {
public interface Sort<T extends Comparable<T>> {

<T extends Comparable<T>> T[] sort(T[] array);
T[] sort(T[] array);
}
27 changes: 21 additions & 6 deletions src/test/java/com/sorts/BubbleSortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,32 @@
class BubbleSortTest {

@Test
void bubbleSortTest() {
BubbleSort bubbleSort = new BubbleSort();
void bubbleSortTestIntegers() {
BubbleSort<Integer> bubbleSort = new BubbleSort<>();

Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7};
Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Integer[] unsortedInt = {0, 5, 9, 2, 1, 3, 4, 8, 6, 7};
Integer[] sortedInt = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Assertions.assertArrayEquals(sortedInt, bubbleSort.sort(unsortedInt));

Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'};
Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
}

@Test
void bubbleSortTestCharacters() {
BubbleSort<Character> bubbleSort = new BubbleSort<>();

Character[] unsortedChar = {'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'};
Character[] sortedChar = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
Assertions.assertArrayEquals(sortedChar, bubbleSort.sort(unsortedChar));

}

@Test
void bubbleSortTestStrings() {
BubbleSort<String> bubbleSort = new BubbleSort<>();

String[] unsortedChar = {"abc", "adc", "bcd", "abb", "abc", "acb"};
String[] sortedChar = {"abb", "abc", "abc", "acb", "adc", "bcd"};
Assertions.assertArrayEquals(sortedChar, bubbleSort.sort(unsortedChar));

}
}