mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 17:56:02 +08:00
Create BucketSortTest (#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>
This commit is contained in:
@ -32,7 +32,7 @@ public class BucketSort {
|
||||
*
|
||||
* @param arr the array contains elements
|
||||
*/
|
||||
private static void bucketSort(int[] arr) {
|
||||
public static int[] bucketSort(int[] arr) {
|
||||
/* get max value of arr */
|
||||
int max = max(arr);
|
||||
|
||||
@ -67,6 +67,8 @@ public class BucketSort {
|
||||
arr[index++] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
/**
|
||||
|
48
src/test/java/com/thealgorithms/sorts/BucketSortTest.java
Normal file
48
src/test/java/com/thealgorithms/sorts/BucketSortTest.java
Normal file
@ -0,0 +1,48 @@
|
||||
package com.thealgorithms.sorts;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BucketSortTest {
|
||||
|
||||
@Test
|
||||
public void bucketSortSingleIntegerArray() {
|
||||
int[] inputArray = { 4 };
|
||||
int[] outputArray = BucketSort.bucketSort(inputArray);
|
||||
int[] expectedOutput = { 4 };
|
||||
assertArrayEquals(outputArray, expectedOutput);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bucketSortNonDuplicateIntegerArray() {
|
||||
int[] inputArray = { 6, 1, 99, 27, 15, 23, 36 };
|
||||
int[] outputArray = BucketSort.bucketSort(inputArray);
|
||||
int[] expectedOutput = {1, 6, 15, 23, 27, 36, 99};
|
||||
assertArrayEquals(outputArray, expectedOutput);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bucketSortDuplicateIntegerArray() {
|
||||
int[] inputArray = { 6, 1, 27, 15, 23, 27, 36, 23 };
|
||||
int[] outputArray = BucketSort.bucketSort(inputArray);
|
||||
int[] expectedOutput = {1, 6, 15, 23, 23, 27, 27, 36};
|
||||
assertArrayEquals(outputArray, expectedOutput);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bucketSortNonDuplicateIntegerArrayWithNegativeNum() {
|
||||
int[] inputArray = { 6, -1, 99, 27, -15, 23, -36 };
|
||||
int[] outputArray = BucketSort.bucketSort(inputArray);
|
||||
int[] expectedOutput = { -36, -15, -1, 6, 23, 27, 99};
|
||||
assertArrayEquals(outputArray, expectedOutput);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bucketSortDuplicateIntegerArrayWithNegativeNum() {
|
||||
int[] inputArray = { 6, -1, 27, -15, 23, 27, -36, 23 };
|
||||
int[] outputArray = BucketSort.bucketSort(inputArray);
|
||||
int[] expectedOutput = { -36, -15, -1, 6, 23, 23, 27, 27};
|
||||
assertArrayEquals(outputArray, expectedOutput);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user