public static void partitionSort(List<Map.Entry<String, ? extends Comparable>> arr, int k) {
        int start = 0;
        int end = arr.size() - 1;
        int target = k - 1;
        while (start < end) {
            int lo = start;
            int hi = end;
            int mid = lo;
            var pivot = arr.get(hi).getValue();
            while (mid <= hi) {
                int cmp = pivot.compareTo(arr.get(mid).getValue());
                if (cmp < 0) {
                    var tmp = arr.get(lo);
                    arr.set(lo++, arr.get(mid));
                    arr.set(mid++, tmp);
                } else if (cmp > 0) {
                    var tmp = arr.get(mid);
                    arr.set(mid, arr.get(hi));
                    arr.set(hi--, tmp);
                } else {
                    mid++;
                }
            }
            if (lo <= target && target < mid) {
                end = lo;
                break;
            }
            if (target < lo) {
                end = lo - 1;
            } else {
                start = mid;
            }
        }
        Collections.sort(arr.subList(0, end), (a, b) -> b.getValue().compareTo(a.getValue()));
    }
@Test
    public void testPartitionSort() {

        Random rand = new Random();
        List<Map.Entry<String, ? extends Comparable>> actual = new ArrayList<>();
        List<Map.Entry<String, ? extends Comparable>> expected = new ArrayList<>();

        for (int j = 0; j < 100; j++) {
            Map<String, Integer> map = new HashMap<>();
            int max = rand.nextInt(10) + 1;
            for (int i = 0; i < max; i++) {
                int val = rand.nextInt(max);
                map.put("" + i, val);
            }
            actual.clear();
            expected.clear();
            for (var etr : map.entrySet()) {
                actual.add(etr);
                expected.add(etr);
            }
            int topk = rand.nextInt(max) + 1;
            TopKBundles.partitionSort(actual, topk);
            Collections.sort(expected, (a, b) -> b.getValue().compareTo(a.getValue()));
            String errorMsg = null;
            for (int i = 0; i < topk; i++) {
                Integer l = (Integer) actual.get(i).getValue();
                Integer r = (Integer) expected.get(i).getValue();
                if (!l.equals(r)) {
                    errorMsg = String.format("Diff found at i=%d, %d != %d, actual:%s, expected:%s",
                            i, l, r, actual, expected);
                }
                assertNull(errorMsg);
            }
        }
    }