Skip to content

Commit e2e8337

Browse files
update bubble sort (#187)
update quick sort
1 parent cdf9353 commit e2e8337

File tree

4 files changed

+220
-0
lines changed

4 files changed

+220
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.examplehub.sorts;
2+
3+
import com.examplehub.utils.SortUtils;
4+
5+
public class BubbleSortV2 implements Sort {
6+
7+
/**
8+
* BubbleSort algorithm implements.
9+
*
10+
* @param numbers the numbers to be sorted.
11+
*/
12+
public void sort(int[] numbers) {
13+
int n = numbers.length - 1;
14+
while (n != 0) {
15+
int lastSwapIdx = 0;
16+
for (int i = 0; i < n; ++i) {
17+
if (numbers[i] > numbers[i + 1]) {
18+
SortUtils.swap(numbers, i, i + 1);
19+
lastSwapIdx = i;
20+
}
21+
}
22+
n = lastSwapIdx;
23+
}
24+
}
25+
26+
/**
27+
* Generic BubbleSort algorithm implements.
28+
*
29+
* @param array the array to be sorted.
30+
* @param <T> the class of the objects in the array.
31+
*/
32+
public <T extends Comparable<T>> void sort(T[] array) {
33+
int n = array.length - 1;
34+
while (n != 0) {
35+
int lastSwapIdx = 0;
36+
for (int i = 0; i < n; ++i) {
37+
if (array[i].compareTo(array[i + 1]) > 0) {
38+
SortUtils.swap(array, i, i + 1);
39+
lastSwapIdx = i;
40+
}
41+
}
42+
n = lastSwapIdx;
43+
}
44+
}
45+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.examplehub.sorts;
2+
3+
import com.examplehub.utils.SortUtils;
4+
5+
/**
6+
* https://en.wikipedia.org/wiki/Quicksort#Lomuto_partition_scheme
7+
*/
8+
public class QuickSortLomutoPartition implements Sort {
9+
10+
@Override
11+
public void sort(int[] numbers) {
12+
quickSort(numbers, 0, numbers.length - 1);
13+
}
14+
15+
private int partition(int[] number, int left, int right) {
16+
int pivot = number[right];
17+
for (int j = left; j < right; ++j) {
18+
if (number[j] < pivot) {
19+
SortUtils.swap(number, left++, j);
20+
}
21+
}
22+
SortUtils.swap(number, left, right);
23+
return left;
24+
}
25+
/**
26+
* QuickSort algorithm implements.
27+
*
28+
* @param numbers the numbers to be sorted.
29+
*/
30+
public void quickSort(int[] numbers, int left, int right) {
31+
if (left < right) {
32+
int pivotIndex = partition(numbers, left, right);
33+
quickSort(numbers, left, pivotIndex - 1);
34+
quickSort(numbers, pivotIndex + 1, right);
35+
}
36+
}
37+
38+
/**
39+
* Generic quickSort algorithm implements.
40+
*
41+
* @param array the array to be sorted.
42+
* @param <T> the class of the objects in the array.
43+
*/
44+
@Override
45+
public <T extends Comparable<T>> void sort(T[] array) {
46+
quickSort(array, 0, array.length - 1);
47+
}
48+
49+
private static <T extends Comparable<T>> int partition(T[] array, int left, int right) {
50+
T pivot = array[right];
51+
for (int j = left; j < right; j++) {
52+
if (array[j].compareTo(pivot) < 0) {
53+
SortUtils.swap(array, j, left++);
54+
}
55+
}
56+
SortUtils.swap(array, left, right);
57+
return left;
58+
}
59+
60+
public static <T extends Comparable<T>> void quickSort(T[] array, int left, int right) {
61+
if (left < right) {
62+
int pivotIndex = partition(array, left, right);
63+
quickSort(array, left, pivotIndex - 1);
64+
quickSort(array, pivotIndex + 1, right);
65+
}
66+
}
67+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.examplehub.sorts;
2+
3+
import com.examplehub.domain.Student;
4+
import com.examplehub.utils.RandomUtils;
5+
import com.examplehub.utils.SortUtils;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.util.Arrays;
10+
11+
import static org.junit.jupiter.api.Assertions.*;
12+
13+
class BubbleSortV2Test {
14+
15+
private Sort sort;
16+
17+
@BeforeEach
18+
public void before() {
19+
sort = new BubbleSortV2();
20+
}
21+
22+
@Test
23+
void testSort() {
24+
int[] ints = RandomUtils.randomInts(-50, 50, 100);
25+
sort.sort(ints);
26+
assertTrue(SortUtils.isSorted(ints));
27+
}
28+
29+
@Test
30+
void testSortIntegers() {
31+
Integer[] integers =
32+
Arrays.stream(RandomUtils.randomInts(-50, 50, 100)).boxed().toArray(Integer[]::new);
33+
sort.sort(integers);
34+
assertTrue(SortUtils.isSorted(integers));
35+
}
36+
37+
@Test
38+
void testSortedDoubles() {
39+
Double[] doubles = new Double[100];
40+
double[] tempDoubles = RandomUtils.randomDoubles(-50, 50, 100);
41+
for (int i = 0; i < doubles.length; ++i) {
42+
doubles[i] = tempDoubles[i];
43+
}
44+
sort.sort(doubles);
45+
assertTrue(SortUtils.isSorted(doubles));
46+
}
47+
48+
@Test
49+
void testComparable() {
50+
Student[] students = new Student[5];
51+
students[0] = new Student(1, 98, 99, 100);
52+
students[1] = new Student(2, 100, 99, 98);
53+
students[2] = new Student(3, 100, 98, 99);
54+
students[3] = new Student(4, 100, 100, 100);
55+
students[4] = new Student(5, 99, 99, 99);
56+
sort.sort(students);
57+
assertTrue(SortUtils.isSorted(students));
58+
}
59+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.*;
11+
12+
class QuickSortLomutoPartitionTest {
13+
private Sort sort;
14+
15+
@BeforeEach
16+
public void before() {
17+
sort = new QuickSortLomutoPartition();
18+
}
19+
20+
@Test
21+
void testQuickSort() {
22+
int[] ints = RandomUtils.randomInts(-50, 50, 100);
23+
sort.sort(ints);
24+
assertTrue(SortUtils.isSorted(ints));
25+
26+
ints = new int[] {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
27+
sort.sort(ints);
28+
assertTrue(SortUtils.isSorted(ints));
29+
}
30+
31+
@Test
32+
void testSortIntegers() {
33+
Integer[] integers =
34+
Arrays.stream(RandomUtils.randomInts(-50, 50, 100)).boxed().toArray(Integer[]::new);
35+
sort.sort(integers);
36+
assertTrue(SortUtils.isSorted(integers));
37+
}
38+
39+
@Test
40+
void testSortedDoubles() {
41+
Double[] doubles = new Double[100];
42+
double[] tempDoubles = RandomUtils.randomDoubles(-50, 50, 100);
43+
for (int i = 0; i < doubles.length; ++i) {
44+
doubles[i] = tempDoubles[i];
45+
}
46+
sort.sort(doubles);
47+
assertTrue(SortUtils.isSorted(doubles));
48+
}
49+
}

0 commit comments

Comments
 (0)