Add Randomized Quick Sort (#6234)

This commit is contained in:
Vibhu Khera
2025-05-10 03:20:09 +05:30
committed by GitHub
parent 6fe630cdf2
commit b09766ede4
2 changed files with 112 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
package com.thealgorithms.randomized;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
/**
* Unit tests for the RandomizedQuickSort class.
*/
public class RandomizedQuickSortTest {
/**
* Tests sorting of an array with multiple elements, including duplicates.
*/
@Test
public void testRandomizedQuickSortMultipleElements() {
int[] arr = {3, 6, 8, 10, 1, 2, 1};
int[] expected = {1, 1, 2, 3, 6, 8, 10};
RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1);
assertArrayEquals(expected, arr);
}
/**
* Tests sorting of an empty array.
*/
@Test
public void testRandomizedQuickSortEmptyArray() {
int[] arr = {};
int[] expected = {};
RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1);
assertArrayEquals(expected, arr);
}
/**
* Tests sorting of an array with a single element.
*/
@Test
public void testRandomizedQuickSortSingleElement() {
int[] arr = {5};
int[] expected = {5};
RandomizedQuickSort.randomizedQuickSort(arr, 0, arr.length - 1);
assertArrayEquals(expected, arr);
}
}