简体   繁体   English

从数组中返回 K 个最大的元素。 (爪哇)

[英]Returning the K biggest element from an array. (JAVA)

public static Word[] simpleSelect(Word[] array, int k){
    k= array.length;
    for(int i = 0; i< k-1; i++){
        for(int j = 0; j<k-i-1; j++){
            if(array[j].compareTo(array[j+1]) < 0){
                swap(array,j, j+1); 
            }
        }           
    }
    return array;
}

I created this above code to return the K biggest elements from an array through a bubble sort.我创建了上面的代码来通过冒泡排序从数组中返回K最大的元素。 I am suppose to make this method O(nk) .我想使这个方法O(nk) I have wrote this code and figured that the returned array doesn't print an array with k size.我写了这段代码,并认为返回的数组不会打印大小为k的数组。 Instead, it just prints the original array with the same length, just bubble sorted.相反,它只是打印具有相同长度的原始数组,只是冒泡排序。

For example, if I my original array is {1,19, 7 ,26 ,9 ,85} and my k value is 2 , I want it to return {85,26} .例如,如果我的原始数组是{1,19, 7 ,26 ,9 ,85}并且我的k值为2 ,我希望它返回{85,26} Instead, currently this code is returning {85,26,19,9,7,1} no matter what the k value is.相反,无论k值是多少,当前这段代码都返回{85,26,19,9,7,1}

I would like to know what i am doing wrong here and also would like to know if I am coding right in O(nk) times.我想知道我在这里做错了什么,也想知道我是否在O(nk)次编码正确。

  1. k is re-assigned on line 2: k = array.length; k在第 2 行重新赋值: k = array.length; it's the reason why the method behaves regardless of the value of k .这就是为什么该方法的行为与k的值无关的原因。
  2. As for complexity bubble sort has an average complexity of O(n^2), it must be adapted to meet your O(nk) requirements.至于复杂度冒泡排序的平均复杂度为 O(n^2),它必须进行调整以满足您的 O(nk) 要求。

Assume we have the following array of numbers假设我们有以下数字数组

static int[] arr = {456, 12 , 998, 546, 12,  987, 6456, 66, 9789};

We are going to construct the array of k largest numbers by iteratively finding the largest number of the array and then marking that number so that it does not get picked again.我们将通过迭代查找数组的最大数然后标记该数以使其不会再次被选中来构建 k 个最大数的数组。

This is the method that will find the k largest elements:这是将找到 k 个最大元素的方法:

private static int[] kLargest(int[] arr, int k){
    int[] klargest = new int[k];
    for(int i=0;i<k;i++){
        klargest[i] = findAndMarkLargest(arr);
    }
    return klargest;
}

And this is the method that find the single largest element, and then marks it (by setting the elements to Integer.MIN_VALUE)这是找到单个最大元素,然后标记它的方法(通过将元素设置为Integer.MIN_VALUE)

private static int findAndMarkLargest(int[] arr){
    int largest = Integer.MIN_VALUE;
    for(int i=0;i<arr.length;i++){
        if(arr[i] > largest){
            largest = arr[i];
        }
    }
    for(int i=0;i<arr.length;i++){
        if(arr[i] == largest){
            arr[i] = Integer.MIN_VALUE;
        }
    }
    return largest;
}

The main method (that simply calls the kLargest method) looks like this: main 方法(简单地调用 kLargest 方法)如下所示:

public static void main (String[] args) throws java.lang.Exception
{
    // your code goes here
    System.out.println(Arrays.toString(kLargest(arr, 3)));
}

And outputs:和输出:

[9789, 6456, 998] [9789、6456、998]

Each call to findAndMarkLargest takes 2*n operations (since it runs over the array twice).每次调用findAndMarkLargest需要 2*n 次操作(因为它在数组上运行两次)。

The method findAndMarkLargest is called k times by 'kLargest'.方法findAndMarkLargest被“kLargest”调用 k 次。 So, in terms of big O notation, this is O(2kn) which is equivalent to O(nk)所以,就大 O 表示法而言,这是 O(2kn) 等价于 O(nk)

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

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