Skip to content

Add quick sort tests #3165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions src/main/java/com/thealgorithms/sorts/QuickSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,4 @@ private static <T extends Comparable<T>> int partition(T[] array, int left, int
}
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 changes: 69 additions & 0 deletions src/test/java/com/thealgorithms/sorts/QuickSortTest.java
Original file line number Diff line number Diff line change
@@ -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);
}

}