查找数组中的 K 个最小元素

Find K Smallest Elements in an Array

我想在一个数组中找到 K 个最小的元素,并且我能够基本上将我的数组排序为最小堆结构,但我仍然得到错误的输出。

这是我的输入:

arr = [9,4,7,1,-2,6,5]
k = 3

代码如下:

public static int[] findKSmallest(int[] arr, int k) {
    int[] result = new int[k];
    int heapSize = arr.length;
    // Write - Your - Code  

    for (int i = (heapSize - 1) / 2; i >= 0; i--) {
        minHeap(arr, i, heapSize);
    }

    for (int j = 0; j < k; j++) {
        result[j] = arr[j];
    }
    return result;
}
public static void minHeap(int[] arr, int index, int heapSize) {
    int smallest = index;
    while (smallest < heapSize / 2) {
        int left = (2 * index) + 1; // 1 more than half the index
        int right = (2 * index) + 2; // 2 more than half the index 

        if (left < heapSize && arr[left] < arr[index]) {
            smallest = left;
        }

        if (right < heapSize && arr[right] < arr[smallest]) {
            smallest = right;
        }

        if (smallest != index) {
            int temp = arr[index];
            arr[index] = arr[smallest];
            arr[smallest] = temp;
            index = smallest;
        } else {
            break;
        }
    }
}

这是我的预期输出:

[-2,1,4]

尽管我的输出是:

[-2,1,5]

请问我哪里出错了

构建 minHeap 后,您必须提取元素并适当调整树。您只是通过索引从数组中获取元素,这不是堆的工作方式。

我稍微修改了你的 minHeap() 方法以使用 recursion,你应该检查 arr[smallest] < arr[left] 而不是相反。

public static int[] findKSmallest(int[] arr, int k) {
    int[] result = new int[k];
    int heapSize = arr.length;
    // Write - Your - Code

    for (int i = (heapSize - 1) / 2; i >= 0; i--) {
        minHeap(arr, heapSize, i);
    }

    // extract elements from heap
    for (int i = heapSize - 1; i > 0; i--) {
        int temp = arr[0];
        arr[0] = arr[i];
        arr[i] = temp;
        minHeap(arr, 0, i);
    }

    for (int j = 0; j < k; j++) {
        result[j] = arr[j];
    }
    return result;
}
public static void minHeap(int[] arr, int index, int heapSize) {
    int smallest = index;
    int left = (2 * index) + 1; // 1 more than half the index
    int right = (2 * index) + 2; // 2 more than half the index
    if (left < heapSize && arr[smallest] < arr[left]) {
        smallest = left;
    }
    if (right < heapSize && arr[smallest] < arr[right]) {
        smallest = right;
    }
    if (smallest != index) {
        int temp = arr[index];
        arr[index] = arr[smallest];
        arr[smallest] = temp;
        minHeap(arr, smallest, heapSize);
    }
}

希望这对您有所帮助。正如预期的那样,结果是:

[-2, 1, 4]

因为Java8我们可以做Sorting stream

替代代码:

Arrays.stream(arr).sorted().boxed().collect(Collectors.toList()).subList(0, k);

其中:

int[] arr = {9,4,7,1,-2,6,5};
int k = 3; // toIndex

上下文和测试平台中的替代代码:

public static void main(String[] args) {
    int[] arr = {9, 4, 7, 1, -2, 6, 5};
    int k = 3; // toIndex

    List<Integer> listResult = Arrays.stream(arr)
            .sorted().boxed().collect(Collectors.toList()).subList(0, k);
    // print out list result
    System.out.println("Output of list result: " + listResult);


    int[] arrayResult = listResult.stream().mapToInt(i -> i).toArray();
    // print out array result
    List<String> arrayResultAsString = Arrays.stream(arrayResult)
            .boxed().map(i -> String.valueOf(i)).collect(Collectors.toList());
    System.out.println("Output of array result: " + arrayResultAsString);
}

输出:

Output of list result: [-2, 1, 4]
Output of array result: [-2, 1, 4]

您可以使用Arrays.stream​(int[])方法:

int[] arr = {9, 4, 7, 1, -2, 6, 5};
int k = 3;

int[] minHeap = Arrays.stream(arr).sorted().limit(k).toArray();

System.out.println(Arrays.toString(minHeap)); // [-2, 1, 4]

如果 K 相对于数组大小而言我的建议是使用选择排序,因此它可能是一种设计模式策略 - 取决于 K 与数组大小的关系,例如两个过程:小 K 的选择排序和对巨大的 K 进行快速排序。如果语句例如选择<=30%<快速