Skip to content

Commit e8409cc

Browse files
Bubble Sort
SortUtils RandomUtils
1 parent 4fa1c59 commit e8409cc

File tree

6 files changed

+235
-0
lines changed

6 files changed

+235
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.examplehub.sorts;
2+
3+
import java.util.Objects;
4+
5+
public class BubbleSort {
6+
7+
/**
8+
* BubbleSort algorithm implements.
9+
*
10+
* @param numbers the numbers to be sorted.
11+
*/
12+
public static void sort(int[] numbers) {
13+
for (int i = 0; i < numbers.length - 1; ++i) {
14+
boolean swapped = false;
15+
for (int j = 0; j < numbers.length - 1 - i; ++j) {
16+
if (numbers[j] > numbers[j + 1]) {
17+
int temp = numbers[j];
18+
numbers[j] = numbers[j + 1];
19+
numbers[j + 1] = temp;
20+
swapped = true;
21+
}
22+
}
23+
if (!swapped) {
24+
break;
25+
}
26+
}
27+
}
28+
29+
/**
30+
* Generic BubbleSort algorithm implements.
31+
*
32+
* @param array the array to be sorted.
33+
* @param <T> the class of the objects in the array.
34+
*/
35+
public static <T extends Comparable<T>> void sort(T[] array) {
36+
for (int i = 0; i < array.length - 1; ++i) {
37+
boolean swapped = false;
38+
for (int j = 0; j < array.length - 1 - i; ++j) {
39+
if (array[j].compareTo(array[j + 1]) > 0) {
40+
T temp = array[j];
41+
array[j] = array[j + 1];
42+
array[j + 1] = temp;
43+
swapped = true;
44+
}
45+
}
46+
if (!swapped) {
47+
break;
48+
}
49+
}
50+
}
51+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.examplehub.utils;
2+
3+
import java.util.Random;
4+
5+
public class RandomUtils {
6+
7+
/**
8+
* Generate random int numbers in a range.
9+
*
10+
* @param min the min value of random numbers.
11+
* @param max the max value of random numbers.
12+
* @param count the count of random numbers.
13+
* @return {@code count} random numbers from {@code min} to {@code max} range.
14+
*/
15+
public static int[] randomInts(int min, int max, int count) {
16+
if (min > max) {
17+
throw new IllegalArgumentException("min value must be less than or equals to max");
18+
}
19+
int[] ints = new int[count];
20+
Random random = new Random();
21+
for (int i = 0; i < count; ++i) {
22+
ints[i] = random.nextInt(max - min + 1) + min;
23+
}
24+
return ints;
25+
}
26+
27+
/**
28+
* Generate random double numbers in a range.
29+
*
30+
* @param min the min value of random numbers.
31+
* @param max the max value of random numbers.
32+
* @param count the count of random numbers.
33+
* @return {@code count} random numbers from {@code min} to {@code max} range.
34+
*/
35+
public static double[] randomDoubles(double min, double max, int count) {
36+
if (min > max) {
37+
throw new IllegalArgumentException("min value must be less than or equals to max");
38+
}
39+
double[] floats = new double[count];
40+
Random random = new Random();
41+
for (int i = 0; i < count; ++i) {
42+
floats[i] = min + random.nextFloat() * (max - min);
43+
}
44+
return floats;
45+
}
46+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.examplehub.utils;
2+
3+
public class SortUtils {
4+
5+
/**
6+
* Test if the array is sorted.
7+
*
8+
* @param array the array to be check.
9+
* @return {@code true} if given array is sorted, otherwise {@code false}.
10+
*/
11+
public static boolean isSorted(int[] array) {
12+
for (int i = 0; i < array.length - 1; ++i) {
13+
if (array[i] > array[i + 1]) {
14+
return false;
15+
}
16+
}
17+
return true;
18+
}
19+
20+
/**
21+
* Test if the generic array is sorted.
22+
*
23+
* @param array the array to be checked.
24+
* @param <T> the class of the objects in the array.
25+
* @return {@code true} if given array is sorted, otherwise {@code true}.
26+
*/
27+
public static <T extends Comparable<T>> boolean isSorted(T[] array) {
28+
for (int i = 0; i < array.length - 1; ++i) {
29+
if (array[i].compareTo(array[i + 1]) > 0) {
30+
return false;
31+
}
32+
}
33+
return true;
34+
}
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.examplehub.sorts;
2+
3+
import com.examplehub.utils.RandomUtils;
4+
import com.examplehub.utils.SortUtils;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.Arrays;
8+
9+
import static org.junit.jupiter.api.Assertions.assertTrue;
10+
11+
class BubbleSortTest {
12+
13+
@Test
14+
void testSort() {
15+
int[] ints = RandomUtils.randomInts(-50, 50, 100);
16+
BubbleSort.sort(ints);
17+
assertTrue(SortUtils.isSorted(ints));
18+
}
19+
20+
@Test
21+
void testSortIntegers() {
22+
23+
Integer[] integers = Arrays.stream(RandomUtils.randomInts(-50, 50, 100))
24+
.boxed().toArray(Integer[]::new);
25+
BubbleSort.sort(integers);
26+
assertTrue(SortUtils.isSorted(integers));
27+
}
28+
29+
@Test
30+
void testSortedDoubles() {
31+
Double[] doubles = new Double[100];
32+
double[] tempDoubles = RandomUtils.randomDoubles(-50, 50, 100);
33+
for (int i = 0; i < doubles.length; ++i) {
34+
doubles[i] = tempDoubles[i];
35+
}
36+
BubbleSort.sort(doubles);
37+
assertTrue(SortUtils.isSorted(doubles));
38+
}
39+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.examplehub.utils;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class RandomUtilsTest {
8+
9+
@Test
10+
void testRandomInts() {
11+
12+
try {
13+
int[] ints = RandomUtils.randomInts(50, -50, 10);
14+
fail(); /* will not happen */
15+
} catch (IllegalArgumentException e) {
16+
assertTrue(true); /* will happen */
17+
}
18+
19+
int[] ints = RandomUtils.randomInts(-50, 50, 100);
20+
for (int anInt : ints) {
21+
assertTrue(anInt >= -50 && anInt <= 50);
22+
}
23+
assertTrue(true);
24+
}
25+
26+
@Test
27+
void testRandomDoubles() {
28+
try {
29+
double[] ints = RandomUtils.randomDoubles(50, -50, 10);
30+
fail(); /* will not happen */
31+
} catch (IllegalArgumentException e) {
32+
assertTrue(true); /* will happen */
33+
}
34+
35+
double[] doubles = RandomUtils.randomDoubles(-50, 50, 100);
36+
for (double anDouble : doubles) {
37+
assertTrue(anDouble >= -50 && anDouble <= 50);
38+
}
39+
assertTrue(true);
40+
41+
}
42+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.examplehub.utils;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.stream.IntStream;
6+
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
8+
9+
class SortUtilsTest {
10+
11+
@Test
12+
void testIsSorted() {
13+
int[] ints = IntStream.range(-50, 50).toArray();
14+
assertTrue(SortUtils.isSorted(ints));
15+
}
16+
17+
@Test
18+
void testIsSortedGeneric() {
19+
Integer[] integers = IntStream.range(-50, 50).boxed().toArray(Integer[]::new);
20+
assertTrue(SortUtils.isSorted(integers));
21+
}
22+
}

0 commit comments

Comments
 (0)