@@ -14,37 +14,50 @@ public void testFindKthMaxTypicalCases() {
1414 assertEquals (3 , FindKthNumber .findKthMax (array1 , 3 ));
1515 assertEquals (4 , FindKthNumber .findKthMax (array1 , 2 ));
1616 assertEquals (5 , FindKthNumber .findKthMax (array1 , 1 ));
17+ assertEquals (3 , FindKthNumber .findKthMaxUsingHeap (array1 , 3 ));
18+ assertEquals (4 , FindKthNumber .findKthMaxUsingHeap (array1 , 2 ));
19+ assertEquals (5 , FindKthNumber .findKthMaxUsingHeap (array1 , 1 ));
1720
1821 int [] array2 = {7 , 5 , 8 , 2 , 1 , 6 };
1922 assertEquals (5 , FindKthNumber .findKthMax (array2 , 4 ));
2023 assertEquals (6 , FindKthNumber .findKthMax (array2 , 3 ));
2124 assertEquals (8 , FindKthNumber .findKthMax (array2 , 1 ));
25+ assertEquals (5 , FindKthNumber .findKthMaxUsingHeap (array2 , 4 ));
26+ assertEquals (6 , FindKthNumber .findKthMaxUsingHeap (array2 , 3 ));
27+ assertEquals (8 , FindKthNumber .findKthMaxUsingHeap (array2 , 1 ));
2228 }
2329
2430 @ Test
2531 public void testFindKthMaxEdgeCases () {
2632 int [] array1 = {1 };
2733 assertEquals (1 , FindKthNumber .findKthMax (array1 , 1 ));
34+ assertEquals (1 , FindKthNumber .findKthMaxUsingHeap (array1 , 1 ));
2835
2936 int [] array2 = {5 , 3 };
3037 assertEquals (5 , FindKthNumber .findKthMax (array2 , 1 ));
3138 assertEquals (3 , FindKthNumber .findKthMax (array2 , 2 ));
39+ assertEquals (5 , FindKthNumber .findKthMaxUsingHeap (array2 , 1 ));
40+ assertEquals (3 , FindKthNumber .findKthMaxUsingHeap (array2 , 2 ));
3241 }
3342
3443 @ Test
3544 public void testFindKthMaxInvalidK () {
3645 int [] array = {1 , 2 , 3 , 4 , 5 };
3746 assertThrows (IllegalArgumentException .class , () -> FindKthNumber .findKthMax (array , 0 ));
3847 assertThrows (IllegalArgumentException .class , () -> FindKthNumber .findKthMax (array , 6 ));
48+ assertThrows (IllegalArgumentException .class , () -> FindKthNumber .findKthMaxUsingHeap (array , 0 ));
49+ assertThrows (IllegalArgumentException .class , () -> FindKthNumber .findKthMaxUsingHeap (array , 6 ));
3950 }
4051
4152 @ Test
4253 public void testFindKthMaxLargeArray () {
4354 int [] array = generateArray (1000 );
4455 int k = new Random ().nextInt (1 , array .length );
4556 int result = FindKthNumber .findKthMax (array , k );
57+ int maxK = FindKthNumber .findKthMaxUsingHeap (array , k );
4658 Arrays .sort (array );
4759 assertEquals (array [array .length - k ], result );
60+ assertEquals (array [array .length - k ], maxK );
4861 }
4962
5063 public static int [] generateArray (int capacity ) {
0 commit comments