Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5fee204

Browse files
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
1 parent 45275ee commit 5fee204

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.thealgorithms.sorts;
2+
3+
import java.util.PriorityQueue;
4+
5+
/**
6+
* Sorts an array using Java's PriorityQueue (Min-Heap).
7+
*
8+
* <p>Example: Input: [7, 2, 9, 4, 1] Output: [1, 2, 4, 7, 9]
9+
*
10+
* <p>Time Complexity:
11+
* - Inserting n elements into the PriorityQueue → O(n log n)
12+
* - Polling n elements → O(n log n)
13+
* - Total: O(n log n)
14+
*
15+
* <p>Space Complexity: O(n) for the PriorityQueue
16+
*
17+
* @see <a href="https://en.wikipedia.org/wiki/Heap_(data_structure)">
18+
* Heap / PriorityQueue</a>
19+
*/
20+
public final class PriorityQueueSort {
21+
22+
// Private constructor to prevent instantiation (utility class)
23+
private PriorityQueueSort() {
24+
}
25+
26+
/**
27+
* Sorts the given array in ascending order using a PriorityQueue.
28+
*
29+
* @param arr the array to be sorted
30+
* @return the sorted array (in-place)
31+
*/
32+
public static int[] sort(int[] arr) {
33+
if (arr == null || arr.length == 0) {
34+
return arr;
35+
}
36+
37+
PriorityQueue<Integer> pq = new PriorityQueue<>();
38+
for (int num : arr) {
39+
pq.offer(num);
40+
}
41+
42+
int i = 0;
43+
while (!pq.isEmpty()) {
44+
arr[i++] = pq.poll();
45+
}
46+
47+
return arr;
48+
}
49+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.sorts;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class PriorityQueueSortTest {
8+
9+
@Test
10+
void testNullArray() {
11+
int[] input = null;
12+
assertArrayEquals(null, PriorityQueueSort.sort(input));
13+
}
14+
15+
@Test
16+
void testSingleElementArray() {
17+
int[] input = {5};
18+
int[] expected = {5};
19+
assertArrayEquals(expected, PriorityQueueSort.sort(input));
20+
}
21+
22+
@Test
23+
void testSortNormalArray() {
24+
int[] input = {7, 2, 9, 4, 1};
25+
int[] expected = {1, 2, 4, 7, 9};
26+
assertArrayEquals(expected, PriorityQueueSort.sort(input));
27+
}
28+
29+
@Test
30+
void testEmptyArray() {
31+
int[] input = {};
32+
int[] expected = {};
33+
assertArrayEquals(expected, PriorityQueueSort.sort(input));
34+
}
35+
36+
@Test
37+
void testNegativeNumbers() {
38+
int[] input = {3, -1, 2, -5, 0};
39+
int[] expected = {-5, -1, 0, 2, 3};
40+
assertArrayEquals(expected, PriorityQueueSort.sort(input));
41+
}
42+
43+
@Test
44+
void testAlreadySortedArray() {
45+
int[] input = {1, 2, 3, 4, 5};
46+
int[] expected = {1, 2, 3, 4, 5};
47+
assertArrayEquals(expected, PriorityQueueSort.sort(input));
48+
}
49+
50+
@Test
51+
void testArrayWithDuplicates() {
52+
int[] input = {5, 1, 3, 3, 2, 5};
53+
int[] expected = {1, 2, 3, 3, 5, 5};
54+
assertArrayEquals(expected, PriorityQueueSort.sort(input));
55+
}
56+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /