Skip to content

Commit 85c836c

Browse files
Add quick sort tests (TheAlgorithms#3165)
1 parent d8c9c1a commit 85c836c

File tree

2 files changed

+69
-19
lines changed

2 files changed

+69
-19
lines changed

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,4 @@ private static <T extends Comparable<T>> int partition(T[] array, int left, int
7676
}
7777
return left;
7878
}
79-
80-
// Driver Program
81-
public static void main(String[] args) {
82-
83-
// For integer input
84-
Integer[] array = {3, 4, 1, 32, 0, 1, 5, 12, 2, 5, 7, 8, 9, 2, 44, 111, 5};
85-
86-
QuickSort quickSort = new QuickSort();
87-
quickSort.sort(array);
88-
89-
// Output => 0 1 1 2 2 3 4 5 5 5 7 8 9 12 32 44 111
90-
print(array);
91-
92-
String[] stringArray = {"c", "a", "e", "b", "d"};
93-
quickSort.sort(stringArray);
94-
95-
// Output => a b c d e
96-
print(stringArray);
97-
}
9879
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
/**
8+
* @author Akshay Dubey (https://github.com/itsAkshayDubey)
9+
* @see QuickSort
10+
*/
11+
class QuickSortTest {
12+
13+
private QuickSort quickSort = new QuickSort();
14+
15+
@Test
16+
void quickSortEmptyArrayShouldPass()
17+
{
18+
Integer[] array = {};
19+
Integer[] sorted = quickSort.sort(array);
20+
Integer[] expected = {};
21+
assertArrayEquals(expected, sorted);
22+
}
23+
24+
@Test
25+
void quickSortSingleValueArrayShouldPass()
26+
{
27+
Integer[] array = {7};
28+
Integer[] sorted = quickSort.sort(array);
29+
Integer[] expected = {7};
30+
assertArrayEquals(expected, sorted);
31+
}
32+
33+
@Test
34+
void quickSortWithIntegerArrayShouldPass()
35+
{
36+
Integer[] array = {49,4,36,9,144,1};
37+
Integer[] sorted = quickSort.sort(array);
38+
Integer[] expected = {1,4,9,36,49,144};
39+
assertArrayEquals(expected, sorted);
40+
}
41+
42+
@Test
43+
void quickSortForArrayWithNegativeValuesShouldPass()
44+
{
45+
Integer[] array = {49,-36,-144,-49,1,9};
46+
Integer[] sorted = quickSort.sort(array);
47+
Integer[] expected = {-144,-49,-36,1,9,49};
48+
assertArrayEquals(expected, sorted);
49+
}
50+
51+
@Test
52+
void quickSortForArrayWithDuplicateValuesShouldPass()
53+
{
54+
Integer[] array = {36,1,49,1,4,9};
55+
Integer[] sorted = quickSort.sort(array);
56+
Integer[] expected = {1,1,4,9,36,49};
57+
assertArrayEquals(expected, sorted);
58+
}
59+
60+
@Test
61+
void quickSortWithStringArrayShouldPass()
62+
{
63+
String[] array = {"c", "a", "e", "b", "d"};
64+
String[] sorted = quickSort.sort(array);
65+
String[] expected = {"a","b","c","d","e"};
66+
assertArrayEquals(expected, sorted);
67+
}
68+
69+
}

0 commit comments

Comments
 (0)