File tree Expand file tree Collapse file tree
main/java/com/examplehub/sorts
test/java/com/examplehub/sorts Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package com .examplehub .sorts ;
22
3- public class BubbleSort {
3+ public class BubbleSort implements Sort {
44
55 /**
66 * BubbleSort algorithm implements.
77 *
88 * @param numbers the numbers to be sorted.
99 */
10- public static void sort (int [] numbers ) {
10+ public void sort (int [] numbers ) {
1111 for (int i = 0 ; i < numbers .length - 1 ; ++i ) {
1212 boolean swapped = false ;
1313 for (int j = 0 ; j < numbers .length - 1 - i ; ++j ) {
@@ -28,9 +28,9 @@ public static void sort(int[] numbers) {
2828 * Generic BubbleSort algorithm implements.
2929 *
3030 * @param array the array to be sorted.
31- * @param <T> the class of the objects in the array .
31+ * @param <T> the class of the objects in the list .
3232 */
33- public static <T extends Comparable <T >> void sort (T [] array ) {
33+ public <T extends Comparable <T >> void sort (T [] array ) {
3434 for (int i = 0 ; i < array .length - 1 ; ++i ) {
3535 boolean swapped = false ;
3636 for (int j = 0 ; j < array .length - 1 - i ; ++j ) {
Original file line number Diff line number Diff line change 1+ package com .examplehub .sorts ;
2+
3+ public class InsertionSort implements Sort {
4+
5+ /**
6+ * InsertionSort algorithm implements.
7+ *
8+ * @param numbers the numbers to be sorted.
9+ */
10+ public void sort (int [] numbers ) {
11+ for (int i = 1 ; i < numbers .length ; ++i ) {
12+ int j = i - 1 ;
13+ int key = numbers [i ];
14+ while (j >= 0 && key < numbers [j ]) {
15+ numbers [j + 1 ] = numbers [j ];
16+ --j ;
17+ }
18+ if (j != i - 1 ) {
19+ numbers [j + 1 ] = key ;
20+ }
21+ }
22+ }
23+
24+ /**
25+ * Generic InsertionSort algorithm implements.
26+ *
27+ * @param array the array to be sorted.
28+ * @param <T> the class of the objects in the list.
29+ */
30+ public <T extends Comparable <T >> void sort (T [] array ) {
31+ for (int i = 1 ; i < array .length ; ++i ) {
32+ int j = i - 1 ;
33+ T key = array [i ];
34+ while (j >= 0 && key .compareTo (array [j ]) < 0 ) {
35+ array [j + 1 ] = array [j ];
36+ --j ;
37+ }
38+ array [j + 1 ] = key ;
39+ }
40+ }
41+ }
Original file line number Diff line number Diff line change 1+ package com .examplehub .sorts ;
2+
3+ public interface Sort {
4+ void sort (int [] numbers );
5+
6+ <T extends Comparable <T >> void sort (T [] array );
7+ }
Original file line number Diff line number Diff line change 22
33import com .examplehub .utils .RandomUtils ;
44import com .examplehub .utils .SortUtils ;
5+ import org .junit .jupiter .api .BeforeEach ;
56import org .junit .jupiter .api .Test ;
67
78import java .util .Arrays ;
1011
1112class BubbleSortTest {
1213
14+ private Sort sort ;
15+
16+ @ BeforeEach
17+ public void before () {
18+ sort = new BubbleSort ();
19+ }
20+
1321 @ Test
1422 void testSort () {
1523 int [] ints = RandomUtils .randomInts (-50 , 50 , 100 );
16- BubbleSort .sort (ints );
24+ sort .sort (ints );
1725 assertTrue (SortUtils .isSorted (ints ));
1826 }
1927
2028 @ Test
2129 void testSortIntegers () {
22-
2330 Integer [] integers = Arrays .stream (RandomUtils .randomInts (-50 , 50 , 100 ))
2431 .boxed ().toArray (Integer []::new );
25- BubbleSort .sort (integers );
32+ sort .sort (integers );
2633 assertTrue (SortUtils .isSorted (integers ));
2734 }
2835
@@ -33,7 +40,7 @@ void testSortedDoubles() {
3340 for (int i = 0 ; i < doubles .length ; ++i ) {
3441 doubles [i ] = tempDoubles [i ];
3542 }
36- BubbleSort .sort (doubles );
43+ sort .sort (doubles );
3744 assertTrue (SortUtils .isSorted (doubles ));
3845 }
3946}
Original file line number Diff line number Diff line change 1+ package com .examplehub .sorts ;
2+
3+ import com .examplehub .utils .RandomUtils ;
4+ import com .examplehub .utils .SortUtils ;
5+ import org .junit .jupiter .api .BeforeEach ;
6+ import org .junit .jupiter .api .Test ;
7+
8+ import java .util .Arrays ;
9+
10+ import static org .junit .jupiter .api .Assertions .assertTrue ;
11+
12+ class InsertionSortTest {
13+
14+ private Sort sort ;
15+
16+ @ BeforeEach
17+ public void before () {
18+ sort = new InsertionSort ();
19+ }
20+
21+ @ Test
22+ void sort () {
23+ int [] ints = RandomUtils .randomInts (-50 , 50 , 100 );
24+ sort .sort (ints );
25+ assertTrue (SortUtils .isSorted (ints ));
26+ }
27+
28+ @ Test
29+ void sortInteger () {
30+ Integer [] integers = Arrays .stream (RandomUtils .randomInts (-50 , 50 , 100 ))
31+ .boxed ().toArray (Integer []::new );
32+ sort .sort (integers );
33+ assertTrue (SortUtils .isSorted (integers ));
34+ }
35+
36+ @ Test
37+ void sortDouble () {
38+ Double [] doubles = new Double [100 ];
39+ double [] tempDoubles = RandomUtils .randomDoubles (-50 , 50 , 100 );
40+ for (int i = 0 ; i < doubles .length ; ++i ) {
41+ doubles [i ] = tempDoubles [i ];
42+ }
43+ sort .sort (doubles );
44+ assertTrue (SortUtils .isSorted (doubles ));
45+ }
46+ }
You can’t perform that action at this time.
0 commit comments