Add quick sort tests (#3165)
This commit is contained in:
parent
d8c9c1ac85
commit
85c836c795
@ -76,23 +76,4 @@ class QuickSort implements SortAlgorithm {
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
// Driver Program
|
||||
public static void main(String[] args) {
|
||||
|
||||
// For integer input
|
||||
Integer[] array = {3, 4, 1, 32, 0, 1, 5, 12, 2, 5, 7, 8, 9, 2, 44, 111, 5};
|
||||
|
||||
QuickSort quickSort = new QuickSort();
|
||||
quickSort.sort(array);
|
||||
|
||||
// Output => 0 1 1 2 2 3 4 5 5 5 7 8 9 12 32 44 111
|
||||
print(array);
|
||||
|
||||
String[] stringArray = {"c", "a", "e", "b", "d"};
|
||||
quickSort.sort(stringArray);
|
||||
|
||||
// Output => a b c d e
|
||||
print(stringArray);
|
||||
}
|
||||
}
|
||||
|
69
src/test/java/com/thealgorithms/sorts/QuickSortTest.java
Normal file
69
src/test/java/com/thealgorithms/sorts/QuickSortTest.java
Normal file
@ -0,0 +1,69 @@
|
||||
package com.thealgorithms.sorts;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author Akshay Dubey (https://github.com/itsAkshayDubey)
|
||||
* @see QuickSort
|
||||
*/
|
||||
class QuickSortTest {
|
||||
|
||||
private QuickSort quickSort = new QuickSort();
|
||||
|
||||
@Test
|
||||
void quickSortEmptyArrayShouldPass()
|
||||
{
|
||||
Integer[] array = {};
|
||||
Integer[] sorted = quickSort.sort(array);
|
||||
Integer[] expected = {};
|
||||
assertArrayEquals(expected, sorted);
|
||||
}
|
||||
|
||||
@Test
|
||||
void quickSortSingleValueArrayShouldPass()
|
||||
{
|
||||
Integer[] array = {7};
|
||||
Integer[] sorted = quickSort.sort(array);
|
||||
Integer[] expected = {7};
|
||||
assertArrayEquals(expected, sorted);
|
||||
}
|
||||
|
||||
@Test
|
||||
void quickSortWithIntegerArrayShouldPass()
|
||||
{
|
||||
Integer[] array = {49,4,36,9,144,1};
|
||||
Integer[] sorted = quickSort.sort(array);
|
||||
Integer[] expected = {1,4,9,36,49,144};
|
||||
assertArrayEquals(expected, sorted);
|
||||
}
|
||||
|
||||
@Test
|
||||
void quickSortForArrayWithNegativeValuesShouldPass()
|
||||
{
|
||||
Integer[] array = {49,-36,-144,-49,1,9};
|
||||
Integer[] sorted = quickSort.sort(array);
|
||||
Integer[] expected = {-144,-49,-36,1,9,49};
|
||||
assertArrayEquals(expected, sorted);
|
||||
}
|
||||
|
||||
@Test
|
||||
void quickSortForArrayWithDuplicateValuesShouldPass()
|
||||
{
|
||||
Integer[] array = {36,1,49,1,4,9};
|
||||
Integer[] sorted = quickSort.sort(array);
|
||||
Integer[] expected = {1,1,4,9,36,49};
|
||||
assertArrayEquals(expected, sorted);
|
||||
}
|
||||
|
||||
@Test
|
||||
void quickSortWithStringArrayShouldPass()
|
||||
{
|
||||
String[] array = {"c", "a", "e", "b", "d"};
|
||||
String[] sorted = quickSort.sort(array);
|
||||
String[] expected = {"a","b","c","d","e"};
|
||||
assertArrayEquals(expected, sorted);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user