Skip to content

Commit a4d21e2

Browse files
committed
Moving common method to utils class
1 parent e113222 commit a4d21e2

File tree

7 files changed

+71
-59
lines changed

7 files changed

+71
-59
lines changed

Diff for: src/com/deepak/algorithms/Library/ArrayUtils.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Algorithms-In-Java
3+
* ArrayUtils.java
4+
*/
5+
package com.deepak.algorithms.Library;
6+
7+
/**
8+
* Utility class for array related operations
9+
*
10+
* @author Deepak
11+
*/
12+
public abstract class ArrayUtils {
13+
14+
/**
15+
* Method to swap values in a array when two indexes are given
16+
*
17+
* @param values
18+
* @param index1
19+
* @param index2
20+
*/
21+
public static <T> void swap(T[] values, int index1, int index2) {
22+
T temp = values[index1];
23+
values[index1] = values[index2];
24+
values[index2] = temp;
25+
}
26+
27+
}

Diff for: src/com/deepak/algorithms/Sorting/BubbleSort.java

+5-27
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
/**
2-
* Algorithms-in-Java
2+
* Algorithms-In-Java
33
* BubbleSort.java
44
*/
55
package com.deepak.algorithms.Sorting;
66

7-
import java.util.Arrays;
7+
import com.deepak.algorithms.Library.ArrayUtils;
88

99
/**
1010
* Class for BubbleSort implementation
1111
* @author Deepak
1212
*/
1313
public class BubbleSort {
1414

15-
/**
16-
* Main method to start the flow of program
17-
* @param args
18-
*/
19-
public static void main(String[] args) {
20-
int[] valuesToBeSorted = {7, 10, 47, 40, 83, 84, 65, 61, 32, 55, 49, 46, 25, 20, 93, 63, 54, 10};
21-
System.out.println("******************* BUBBLE - SORT *******************");
22-
performBubbleSort(valuesToBeSorted);
23-
}
24-
2515
/**
2616
* Bubble Sort implementation
2717
* <p> Question - When will you consider a list of items to be sorted?
@@ -48,27 +38,15 @@ public static void main(String[] args) {
4838
*
4939
* @param listOfValues - List of values passed in the request
5040
*/
51-
private static void performBubbleSort(int[] listOfValues) {
41+
public static Integer[] performBubbleSort(Integer[] listOfValues) {
5242
for (int i = 0; i < listOfValues.length; i++) {
5343
for (int j = 1; j < (listOfValues.length - i); j++) {
5444
if (listOfValues[j - 1] > listOfValues[j]) {
55-
swap(listOfValues, j - 1, j);
45+
ArrayUtils.swap(listOfValues, j - 1, j);
5646
}
5747
}
5848
}
59-
Arrays.stream(listOfValues).forEach(System.out::println);
60-
}
61-
62-
/**
63-
* Method to swap 2 values
64-
* @param values
65-
* @param firstValue
66-
* @param secondValue
67-
*/
68-
private static void swap(int[] values, int firstValue, int secondValue) {
69-
int tempValue = values[firstValue];
70-
values[firstValue] = values[secondValue];
71-
values[secondValue] = tempValue;
49+
return listOfValues;
7250
}
7351

7452
}

Diff for: src/com/deepak/algorithms/Sorting/InsertionSort.java

+4-15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
package com.deepak.algorithms.Sorting;
66

7+
import com.deepak.algorithms.Library.ArrayUtils;
8+
79
/**
810
* Insertion sort implementation
911
*
@@ -37,28 +39,15 @@ public class InsertionSort {
3739
*
3840
* @param listOfValues - List of values passed in the request
3941
*/
40-
public static int[] performInsertionSort(int[] listOfValues) {
42+
public static Integer[] performInsertionSort(Integer[] listOfValues) {
4143
for (int i = 1; i < listOfValues.length; i++) {
4244
for (int j = i; j > 0; j--) {
4345
if (listOfValues[j] < listOfValues[j - 1]) {
44-
swap(listOfValues, j, j - 1);
46+
ArrayUtils.swap(listOfValues, j, j - 1);
4547
}
4648
}
4749
}
4850
return listOfValues;
4951
}
5052

51-
/**
52-
* Method to swap values at any two indexes
53-
*
54-
* @param values
55-
* @param index1
56-
* @param index2
57-
*/
58-
private static void swap(int[] values, int index1, int index2) {
59-
int tempValue = values[index1];
60-
values[index1] = values[index2];
61-
values[index2] = tempValue;
62-
}
63-
6453
}

Diff for: src/com/deepak/algorithms/Sorting/SelectionSort.java

+4-15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
package com.deepak.algorithms.Sorting;
66

7+
import com.deepak.algorithms.Library.ArrayUtils;
8+
79
/**
810
* Selection Sort implementation
911
*
@@ -37,30 +39,17 @@ public class SelectionSort {
3739
*
3840
* @param listOfValues - List of values passed in the request
3941
*/
40-
public static int[] performSelectionSort(int[] listOfValues) {
42+
public static Integer[] performSelectionSort(Integer[] listOfValues) {
4143
for (int i = 0; i < listOfValues.length - 1; i++) {
4244
int minValue = listOfValues[i];
4345
for (int j = i + 1; j < listOfValues.length; j++) {
4446
if (listOfValues[j] < minValue) {
4547
minValue = listOfValues[j];
46-
swap(listOfValues, i, j);
48+
ArrayUtils.swap(listOfValues, i, j);
4749
}
4850
}
4951
}
5052
return listOfValues;
5153
}
5254

53-
/**
54-
* Method to swap values at any two indexes
55-
*
56-
* @param values
57-
* @param index1
58-
* @param index2
59-
*/
60-
private static void swap(int[] values, int index1, int index2) {
61-
int tempValue = values[index1];
62-
values[index1] = values[index2];
63-
values[index2] = tempValue;
64-
}
65-
6655
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Algorithms-In-Java
3+
* BubbleSort_Test.java
4+
*/
5+
package com.deepak.algorithms.Sorting;
6+
7+
import java.util.Arrays;
8+
9+
import org.junit.Assert;
10+
import org.junit.Test;
11+
12+
/**
13+
* Test cases for Bubble sort
14+
*
15+
* @author Deepak
16+
*/
17+
public class BubbleSort_Test {
18+
19+
/**
20+
* Test case for bubble sort
21+
*/
22+
@Test
23+
public void testBubbleSort() {
24+
Integer[] valuesToBeSorted = {7, 10, 47, 40, 83, 84, 65, 61, 32, 55, 49, 46, 25, 20, 93, 63, 54, 10};
25+
Assert.assertEquals(Arrays.toString(valuesToBeSorted), "[7, 10, 47, 40, 83, 84, 65, 61, 32, 55, 49, 46, 25, 20, 93, 63, 54, 10]");
26+
Assert.assertEquals(Arrays.toString(BubbleSort.performBubbleSort(valuesToBeSorted)), "[7, 10, 10, 20, 25, 32, 40, 46, 47, 49, 54, 55, 61, 63, 65, 83, 84, 93]");
27+
}
28+
29+
}

Diff for: test/com/deepak/algorithms/Sorting/InsertionSort_Test.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class InsertionSort_Test {
2121
*/
2222
@Test
2323
public void testInsertionSort() {
24-
int[] valuesToBeSorted = {7, 10, 47, 40, 83, 84, 65, 61, 32, 55, 49, 46, 25, 20, 93, 63, 54, 10};
24+
Integer[] valuesToBeSorted = {7, 10, 47, 40, 83, 84, 65, 61, 32, 55, 49, 46, 25, 20, 93, 63, 54, 10};
2525
Assert.assertEquals(Arrays.toString(valuesToBeSorted), "[7, 10, 47, 40, 83, 84, 65, 61, 32, 55, 49, 46, 25, 20, 93, 63, 54, 10]");
2626
Assert.assertEquals(InsertionSort.performInsertionSort(valuesToBeSorted), "[7, 10, 10, 20, 25, 32, 40, 46, 47, 49, 54, 55, 61, 63, 65, 83, 84, 93]");
2727
}

Diff for: test/com/deepak/algorithms/Sorting/SelectionSort_Test.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class SelectionSort_Test {
2121
*/
2222
@Test
2323
public void testSelectionSort() {
24-
int[] valuesToBeSorted = {7, 10, 47, 40, 83, 84, 65, 61, 32, 55, 49, 46, 25, 20, 93, 63, 54, 10};
24+
Integer[] valuesToBeSorted = {7, 10, 47, 40, 83, 84, 65, 61, 32, 55, 49, 46, 25, 20, 93, 63, 54, 10};
2525
Assert.assertEquals(Arrays.toString(valuesToBeSorted), "[7, 10, 47, 40, 83, 84, 65, 61, 32, 55, 49, 46, 25, 20, 93, 63, 54, 10]");
2626
Assert.assertEquals(Arrays.toString(SelectionSort.performSelectionSort(valuesToBeSorted)), "[7, 10, 10, 20, 25, 32, 40, 46, 47, 49, 54, 55, 61, 63, 65, 83, 84, 93]");
2727
}

0 commit comments

Comments
 (0)