Skip to content

Commit 56b374c

Browse files
RajkumarRajkumar
Rajkumar
authored and
Rajkumar
committed
Fixed 'Generics' issue of bubble sort since Sort class should accept
only comparable datatype rather than just restricting sort method
1 parent 6ad2c9f commit 56b374c

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

src/main/java/com/sorts/BubbleSort.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.types.Sort;
44

5-
public class BubbleSort<T> implements Sort<T> {
5+
public class BubbleSort<T extends Comparable<T>> implements Sort<T> {
66
/**
77
* This method implements the Generic Bubble Sort
88
*
@@ -11,7 +11,7 @@ public class BubbleSort<T> implements Sort<T> {
1111
**/
1212

1313
@Override
14-
public <T extends Comparable<T>> T[] sort(T[] array) {
14+
public T[] sort(T[] array) {
1515
int last = array.length;
1616
//Sorting
1717
boolean swap;

src/main/java/com/types/DataStructure.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.Iterator;
44

55
/**
6-
* This interface is to define bacis functionality expected out of any implementation class
6+
* This interface is to define basic functionality expected out of any implementation class
77
* Since this is a data structure it should have the flexibility to contain any kind of object hence it has been made generic
88
* Any implementation class need not to be thread safe or it could be depending on the implementation class how does it want to behave.
99
*

src/main/java/com/types/Sort.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.types;
22

33
@FunctionalInterface
4-
public interface Sort<T> {
4+
public interface Sort<T extends Comparable<T>> {
55

6-
<T extends Comparable<T>> T[] sort(T[] array);
6+
T[] sort(T[] array);
77
}

src/test/java/com/sorts/BubbleSortTest.java

+21-6
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,32 @@
66
class BubbleSortTest {
77

88
@Test
9-
void bubbleSortTest() {
10-
BubbleSort bubbleSort = new BubbleSort();
9+
void bubbleSortTestIntegers() {
10+
BubbleSort<Integer> bubbleSort = new BubbleSort<>();
1111

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

16-
Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'};
17-
Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
16+
}
17+
18+
@Test
19+
void bubbleSortTestCharacters() {
20+
BubbleSort<Character> bubbleSort = new BubbleSort<>();
21+
22+
Character[] unsortedChar = {'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'};
23+
Character[] sortedChar = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
1824
Assertions.assertArrayEquals(sortedChar, bubbleSort.sort(unsortedChar));
1925

2026
}
2127

28+
@Test
29+
void bubbleSortTestStrings() {
30+
BubbleSort<String> bubbleSort = new BubbleSort<>();
31+
32+
String[] unsortedChar = {"abc", "adc", "bcd", "abb", "abc", "acb"};
33+
String[] sortedChar = {"abb", "abc", "abc", "acb", "adc", "bcd"};
34+
Assertions.assertArrayEquals(sortedChar, bubbleSort.sort(unsortedChar));
35+
36+
}
2237
}

0 commit comments

Comments
 (0)