Skip to content

Commit 6c9090f

Browse files
hyeonmin2Debasish Biswas
andauthored
Create BucketSortTest (TheAlgorithms#3779)
* Create BucketSortTest * Update src/test/java/com/thealgorithms/sorts/BucketSortTest.java Co-authored-by: Debasish Biswas <debasishbsws.abc@gmail.com> Co-authored-by: Debasish Biswas <debasishbsws.abc@gmail.com>
1 parent 27fc872 commit 6c9090f

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/main/java/com/thealgorithms/sorts/BucketSort.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static void main(String[] args) {
3232
*
3333
* @param arr the array contains elements
3434
*/
35-
private static void bucketSort(int[] arr) {
35+
public static int[] bucketSort(int[] arr) {
3636
/* get max value of arr */
3737
int max = max(arr);
3838

@@ -67,6 +67,8 @@ private static void bucketSort(int[] arr) {
6767
arr[index++] = value;
6868
}
6969
}
70+
71+
return arr;
7072
}
7173

7274
/**
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.thealgorithms.sorts;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class BucketSortTest {
8+
9+
@Test
10+
public void bucketSortSingleIntegerArray() {
11+
int[] inputArray = { 4 };
12+
int[] outputArray = BucketSort.bucketSort(inputArray);
13+
int[] expectedOutput = { 4 };
14+
assertArrayEquals(outputArray, expectedOutput);
15+
}
16+
17+
@Test
18+
public void bucketSortNonDuplicateIntegerArray() {
19+
int[] inputArray = { 6, 1, 99, 27, 15, 23, 36 };
20+
int[] outputArray = BucketSort.bucketSort(inputArray);
21+
int[] expectedOutput = {1, 6, 15, 23, 27, 36, 99};
22+
assertArrayEquals(outputArray, expectedOutput);
23+
}
24+
25+
@Test
26+
public void bucketSortDuplicateIntegerArray() {
27+
int[] inputArray = { 6, 1, 27, 15, 23, 27, 36, 23 };
28+
int[] outputArray = BucketSort.bucketSort(inputArray);
29+
int[] expectedOutput = {1, 6, 15, 23, 23, 27, 27, 36};
30+
assertArrayEquals(outputArray, expectedOutput);
31+
}
32+
33+
@Test
34+
public void bucketSortNonDuplicateIntegerArrayWithNegativeNum() {
35+
int[] inputArray = { 6, -1, 99, 27, -15, 23, -36 };
36+
int[] outputArray = BucketSort.bucketSort(inputArray);
37+
int[] expectedOutput = { -36, -15, -1, 6, 23, 27, 99};
38+
assertArrayEquals(outputArray, expectedOutput);
39+
}
40+
41+
@Test
42+
public void bucketSortDuplicateIntegerArrayWithNegativeNum() {
43+
int[] inputArray = { 6, -1, 27, -15, 23, 27, -36, 23 };
44+
int[] outputArray = BucketSort.bucketSort(inputArray);
45+
int[] expectedOutput = { -36, -15, -1, 6, 23, 23, 27, 27};
46+
assertArrayEquals(outputArray, expectedOutput);
47+
}
48+
}

0 commit comments

Comments
 (0)