Add quick sort tests (#3165)

This commit is contained in:
Akshay Dubey 2022-06-24 11:27:01 +05:30 committed by GitHub
parent d8c9c1ac85
commit 85c836c795
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 19 deletions

View File

@ -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);
}
}

View 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);
}
}