Skip to content

Commit fdbbb9a

Browse files
add quick sort
1 parent 0310217 commit fdbbb9a

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.examplehub.sorts;
2+
3+
import com.examplehub.utils.SortUtils;
4+
5+
public class QuickSort implements Sort {
6+
7+
@Override
8+
public void sort(int[] numbers) {
9+
quickSort(numbers, 0, numbers.length - 1);
10+
}
11+
12+
/**
13+
* QuickSort algorithm implements.
14+
*
15+
* @param numbers the numbers to be sorted.
16+
*/
17+
public void quickSort(int[] numbers, int left, int right) {
18+
if (left >= right) {
19+
return;
20+
}
21+
int pivot = numbers[right]; /* pick last element as pivot */
22+
/* begin partition */
23+
int i = left; /* left pointer */
24+
int j = right - 1; /* right pointer */
25+
while (i <= j) {
26+
while (numbers[i] < pivot) {
27+
i++; /* move i forward */
28+
}
29+
while (j >= 0 && numbers[j] >= pivot) {
30+
j--; /* move j backward */
31+
}
32+
if (i < j) {
33+
SortUtils.swap(numbers, i, j);
34+
i++;
35+
j--;
36+
}
37+
}
38+
39+
SortUtils.swap(numbers, i, right);
40+
41+
quickSort(numbers, left, i - 1);
42+
quickSort(numbers, i + 1, right);
43+
}
44+
45+
@Override
46+
public <T extends Comparable<T>> void sort(T[] array) {
47+
quickSort(array, 0, array.length - 1);
48+
}
49+
50+
public static <T extends Comparable<T>> void quickSort(T[] array, int left, int right) {
51+
if (left >= right) {
52+
return;
53+
}
54+
T pivot = array[right]; /* pick last element as pivot */
55+
/* begin partition */
56+
int i = left; /* left pointer */
57+
int j = right - 1; /* right pointer */
58+
while (i <= j) {
59+
while (array[i].compareTo(pivot) < 0) {
60+
i++; /* move i forward */
61+
}
62+
while (j >= 0 && array[j].compareTo(pivot) >= 0) {
63+
j--; /* move j backward */
64+
}
65+
if (i < j) {
66+
SortUtils.swap(array, i, j);
67+
i++;
68+
j--;
69+
}
70+
}
71+
72+
SortUtils.swap(array, i, right);
73+
74+
quickSort(array, left, i - 1);
75+
quickSort(array, i + 1, right);
76+
}
77+
}

src/main/java/com/examplehub/utils/SortUtils.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,31 @@ public static <T extends Comparable<T>> boolean isSorted(T[] array) {
3232
}
3333
return true;
3434
}
35+
36+
/**
37+
* Swapped array elements at different index.
38+
*
39+
* @param numbers the numbers to be swapped.
40+
* @param i the first index.
41+
* @param j the second index.
42+
*/
43+
public static void swap(int[] numbers, int i, int j) {
44+
int temp = numbers[i];
45+
numbers[i] = numbers[j];
46+
numbers[j] = temp;
47+
}
48+
49+
/**
50+
* Generic swapped array elements at different index.
51+
*
52+
* @param numbers the numbers to be swapped.
53+
* @param i the first index.
54+
* @param j the second index.
55+
* @param <T> the class of the objects in the array.
56+
*/
57+
public static <T extends Comparable<T>> void swap(T[] numbers, int i, int j) {
58+
T temp = numbers[i];
59+
numbers[i] = numbers[j];
60+
numbers[j] = temp;
61+
}
3562
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 QuickSortTest {
13+
14+
private Sort sort;
15+
16+
@BeforeEach
17+
public void before() {
18+
sort = new QuickSort();
19+
}
20+
21+
@Test
22+
void testQuickSort() {
23+
int[] ints = RandomUtils.randomInts(-50, 50, 100);
24+
sort.sort(ints);
25+
assertTrue(SortUtils.isSorted(ints));
26+
}
27+
28+
@Test
29+
void testSortIntegers() {
30+
Integer[] integers = Arrays.stream(RandomUtils.randomInts(-50, 50, 100))
31+
.boxed().toArray(Integer[]::new);
32+
sort.sort(integers);
33+
assertTrue(SortUtils.isSorted(integers));
34+
}
35+
36+
@Test
37+
void testSortedDoubles() {
38+
Double[] doubles = new Double[100];
39+
double[] tempDoubles = RandomUtils.randomDoubles(-50, 50, 100);
40+
for (int i = 0; i < doubles.length; ++i) {
41+
doubles[i] = tempDoubles[i];
42+
}
43+
sort.sort(doubles);
44+
assertTrue(SortUtils.isSorted(doubles));
45+
}
46+
}

0 commit comments

Comments
 (0)