Added PriorityQueueSort algorithm and Tests (#6532)

* Added PriorityQueueSort.java

Implemented PriorityQueueSort using Java's PriorityQueue (Min-Heap).
- Returns the array sorted in ascending order
- Time complexity: O(n log n)
- Space complexity: O(n)

* Added tests for PriorityQueueSort

* Update PriorityQueueSortTest.java

* Fixed formatting and added full coverage tests for PriorityQueueSort

* Update PriorityQueueSort.java

* Update PriorityQueueSort.java

* Update PriorityQueueSort.java

* Update PriorityQueueSortTest.java

* Update PriorityQueueSort.java

* Update PriorityQueueSort.java

* Update PriorityQueueSort.java

* Fix formatting with clang-format
This commit is contained in:
Rahul P
2025-09-18 23:32:15 +05:30
committed by GitHub
parent 45275ee935
commit 5fee204773
2 changed files with 105 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
package com.thealgorithms.sorts;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
class PriorityQueueSortTest {
@Test
void testNullArray() {
int[] input = null;
assertArrayEquals(null, PriorityQueueSort.sort(input));
}
@Test
void testSingleElementArray() {
int[] input = {5};
int[] expected = {5};
assertArrayEquals(expected, PriorityQueueSort.sort(input));
}
@Test
void testSortNormalArray() {
int[] input = {7, 2, 9, 4, 1};
int[] expected = {1, 2, 4, 7, 9};
assertArrayEquals(expected, PriorityQueueSort.sort(input));
}
@Test
void testEmptyArray() {
int[] input = {};
int[] expected = {};
assertArrayEquals(expected, PriorityQueueSort.sort(input));
}
@Test
void testNegativeNumbers() {
int[] input = {3, -1, 2, -5, 0};
int[] expected = {-5, -1, 0, 2, 3};
assertArrayEquals(expected, PriorityQueueSort.sort(input));
}
@Test
void testAlreadySortedArray() {
int[] input = {1, 2, 3, 4, 5};
int[] expected = {1, 2, 3, 4, 5};
assertArrayEquals(expected, PriorityQueueSort.sort(input));
}
@Test
void testArrayWithDuplicates() {
int[] input = {5, 1, 3, 3, 2, 5};
int[] expected = {1, 2, 3, 3, 5, 5};
assertArrayEquals(expected, PriorityQueueSort.sort(input));
}
}