简体   繁体   English

找到数组中的k个最小整数

[英]Find the k smallest integer in an array

Here is my code, it works for finding 1-7 smallest integers, but 8 and 9. It returns null when I find 8 smallest integer in the array. 这是我的代码,它适用于查找1-7个最小整数,但是8和9.当我在数组中找到8个最小整数时,它返回null。 Can anyone help me where is the problem? 任何人都可以帮我解决问题吗? I am using quicksort here. 我在这里使用quicksort。 Thanks very much! 非常感谢!

update: I have figured out the problem, which is the array in the main function. 更新:我已经找到问题,这是主函数中的数组。 After I change to the following look, 在我改为下面的样子后,

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

and

if(front>=end) return input;

It works now! 它现在有效!

import java.util.Arrays;
import java.io.*;

class quicksort{
public static void main(String[] args){
    int[] arr = new int[9];
    arr[0] = 7;
    arr[1] = 2;
    arr[2] = 4;
    arr[3] = 8;
    arr[4] = 3;
    arr[5] = 5;
    arr[6] = 1;
    arr[7] = 0;
    arr[8] = 10;

    System.out.println((Arrays.toString(findKSamllest(arr,8))));
}
public static int partition(int[] input, int front, int end){
    int pivot = input[front];
    while(front < end){
        while(input[front]<pivot)
            front++;
        while(input[end]>pivot)
            end--;
        swap(input,front,end);
    }
    return front;
}
public static void swap(int[] input, int s, int l){
    int temp = input[s];
    input[s] = input[l];
    input[l] = temp;
}

public static int[] findK(int[] input, int front, int end, int k){
    if(front>=end) return null;
    int pivot = partition(input,front,end);
    //System.out.println(pivot);
    if(k==pivot){
        return Arrays.copyOfRange(input,0,pivot);
    }
    else {
        if(k<pivot) 
            return findK(input,front,pivot,k); 
        return findK(input,pivot+1,end,k);
    }
}
public static int[] findKSamllest(int[] input, int k){
    return findK(input, 0, input.length-1, k);
}

} }

Change 更改

if(front >= end) return null;

to

if(front > end) return null;

Stand on the shoulders of giants and use the libraries that are already available: 站在巨人的肩膀上,并使用已有的库:

Arrays.sort(myArray);
int[] returnArray = new int(NUM_ITEMS_TO_RETURN);
for (int i=0; i < NUM_ITEMS_TO_RETURN; i++)
{
   returnArray[i] = myArray[i];
}

return returnArray;

Obviously you have to do some error checking, for example that the initial array is larger or equal to the number of items you want returned, but that's trivial. 显然你必须做一些错误检查,例如初始数组大于或等于你想要返回的项目数,但这是微不足道的。

You could save you a bit time and impress your teacher with the fancy new Java 8 API. 您可以节省一些时间,并使用花哨的新Java 8 API给您的老师留下深刻印象。

It provides streams and useful functions to solve this in one (long) line or 5, if it should be readable ;-) 它提供了流和有用的功能,可以在一条(长)行或5条中解决这个问题,如果它应该是可读的;-)

final List<Integer> sortedKList = Arrays.asList(7, 2, 4, 8, 3, 5, 1, 0, 10)
    .stream()
    .sorted()
    .limit(7)
    .collect(Collectors.toList());

You can then review your result with: 然后,您可以使用以下方式查看结果

sortedKList.forEach(System.out::println);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM