java find the smallest number of k

Title description: Enter N numbers and find the smallest K numbers. For example, input 1, 2, 3, 4, 5, 6, 7, 8, and find the smallest 4 numbers, and output 1, 2, 3, 4.

The solution that is easier to think of : sort the N numbers, and the k numbers at the top after sorting are the smallest k numbers.
Time complexity O(n) solution : only applicable when we can modify the input array

  • Use the Partition function (O(n)) to divide to find the kth number in the array, so that the numbers on the left of the kth number are smaller than the kth number, and the numbers on the right of the kth number are all smaller than the kth number The number is big.
    Code:

public class 最小的k个数 {
    
    
    // 使用partition进行求解,这样的话,比第k小的数字小的都位于数组的左边,比第k个数字大的都位于数组的右边
    private static void getMinKNumbers(int[] input, int k) throws Exception {
    
    
        if (input == null || input.length == 0 || k <= 0) {
    
    
            return;
        }
        int start = 0;
        int end = input.length - 1;
        int index = Partition(input, start, end);
        while (index != (k - 1)) {
    
    
            if (index > k - 1) {
    
    
                end = index - 1;
                index = Partition(input, start, end);
            } else {
    
    
                start = index + 1;
                index = Partition(input, start, end);// 时间复杂度为O(n)
            }
        }
        for (int i = 0; i < k; i++) {
    
    
            System.out.println(input[i]);
        }

    }

    private static int Partition(int[] input, int start, int end) throws Exception {
    
    
        if (input == null || start < 0 || end > input.length) {
    
    
            throw new Exception("in partition function, input param error.");
        }
        int partitionIndex = RandomInRange(start, end); // 随机生成partition的 index 
        int pivort = input[partitionIndex];
        swap(input, partitionIndex, end);
        int small = start - 1;
        for (int i = start; i < end; i++) {
    
    
            if (input[i] < pivort) {
    
    
                small++;
                if (small != i) {
    
    
                    // 如果两个下标相等的话,就不进行交换
                    swap(input, small, i);
                }
            }
        }
        small++;
        swap(input, small, end);
        return small;
    }

    private static void swap(int[] data, int index1, int index2) {
    
    
        int temp = data[index1];
        data[index1] = data[index2];
        data[index2] = temp;
    }


    /*随机生成划分的中心点*/
    private static int RandomInRange(int start, int end) {
    
    
        Random random = new Random();
        // nextInt(n)生成的随机数的范围不包括n,所以我们下面加1。
        return random.nextInt(end - start + 1) + start;
    }


    public static void main(String[] args) throws Exception {
    
    
        getMinKNumbers(new int[]{
    
    4, 5, 1, 6, 2, 7, 3, 8}, 4);
    }
}

Guess you like

Origin blog.csdn.net/liu_12345_liu/article/details/103652957