简体   繁体   English

数组找到第二高的值

[英]array find the second highest value

I have an array contains 10 integer values. 我有一个包含10个整数值的数组。 Now I want to find out the second highest number.I should not use any java API. 现在我想找出第二高的数字。我不应该使用任何java API。 This question was asked by one interviewer to me. 一位采访者向我提出了这个问题。 He wants the logic.And his requirement is, I should not traverse through the the entire elements. 他想要逻辑。他的要求是,我不应该遍历整个元素。 Is there any way where we can achieve the result without traversing? 有没有办法在没有遍历的情况下实现结果? Travesing means going through all the elements in the array. Travesing意味着遍历数组中的所有元素。 I thought for long time.Finally I gave up. 我想了很久。最后我放弃了。 If any one can explain, it would be nice. 如果有人可以解释,那就太好了。 And also I asked about Sorting. 我还询问了排序。 He does not want the array to be sorted. 他不希望对数组进行排序。

No, it's completely impossible. 不,这完全不可能。 If you don't look at all the data, you can't possibly know the second highest value. 如果你没有看所有的数据呢,你不可能知道的第二高值。

You don't have to sort all the data, of course, which may be what your interviewer meant - but you do need to look at every element at least once. 当然,您不必所有数据进行排序 ,这可能是您的面试官的意思 - 但您确实需要至少查看一次所有元素。 Every element has the possibility of changing the result, so needs to be examined. 每个元素都有可能改变结果,因此需要进行检查。

You cannot know the answer if you don't look at each element at least once. 如果你不至少看一次每个元素,你就无法知道答案。 However, if that is not your concern, you could use a Binary Search Tree: 但是,如果您不关心,可以使用二进制搜索树:

In computer science, a binary search tree (BST), which may sometimes also be called an ordered or sorted binary tree, is a node-based binary tree data structure which has the following properties:[1] 在计算机科学中,二进制搜索树(BST)(有时也可称为有序或有序二叉树)是基于节点的二叉树数据结构,具有以下属性:[1]

The left subtree of a node contains only nodes with keys less than the node's key. 节点的左子树仅包含键小于节点键的节点。 The right subtree of a node contains only nodes with keys greater than or equal to the node's key. 节点的右子树仅包含键大于或等于节点键的节点。 Both the left and right subtrees must also be binary search trees. 左右子树也必须是二叉搜索树。
Source: http://en.wikipedia.org/wiki/Binary_search_tree 资料来源: http//en.wikipedia.org/wiki/Binary_search_tree


How will you find 2nd biggest element? 你怎么会找到第二大元素?

Well from how the BST is formed, you know that the biggest element is the rightmost one (starting from the root node, take the right child till you have no place to go any more). 从BST的形成方式来看,你知道最大的元素是最右边的元素(从根节点开始,带着正确的孩子,直到你无处可去)。


Now, how to identify the 2nd biggest? 现在,如何确定第二大?
Well, this may not b the most efficient approach, but let's break it down in cases: 嗯,这可能不是最有效的方法,但让我们在以下情况下将其分解:

  1. Biggest Node is Tree root and has no children - There is no 2nd biggest element because there is only one Node in your Tree 最大的节点是树根并且没有子节点 - 没有第二大元素,因为树中只有一个节点

  2. Biggest Node is Tree root (doesn't have right child) - 2nd biggest element is the biggest element in the left subtree 最大的节点是树根(没有右子) - 第二大元素是左子树中最大的元素

  3. Biggest element is not a Tree root, but not a leaf (has a left child) - 2nd biggest element is his left child 最大元素不是树根,而不是叶子(有一个左子) - 第二大元素是他的左子

  4. Biggest element is not a Tree root, and is a leaf (has no children) - 2nd biggest element is his parent 最大的元素不是树根,而是叶子(没有孩子) - 第二大元素是他的父母

I'm sure you can figure out the pattern and a simple algorithm now :D 我相信你现在可以找出模式和一个简单的算法:D

I know this has been answered but I was thinking something like this. 我知道这已经得到了解答,但我在想这样的事情。

    double maxVal = array[0];
    for (int i=1; i < array.length; i++) {
        if (array[i] > maxVal) {
            maxVal = array[i];
        }
    }

Technically I am not traversing the whole array but n-1 of the length because I assign the first value to the max value. 从技术上讲,我不是遍历整个数组而是遍历长度的n-1,因为我将第一个值分配给最大值。 I am sure there are other ways but this one seems the about what the interviewer may want. 我相信还有其他方法,但这个似乎是面试官可能想要的。

Logic is so simple, I have implemented this already in a single for loop. 逻辑是如此简单,我已经在一个for循环中实现了这一点。

am giving you my program, If you want the algorithm, I can provide you that also. 我给你我的程序,如果你想要算法,我也可以为你提供。

public static void main(String[] args) { public static void main(String [] args){

    int[] arr={0,12,74,56,2,63,45};
    int f1=1,f2=0,temp=0;
    int num=0;

    for(int i=0;i<arr.length;i++){
        num=arr[i];
        if(f1<num)
        {
            temp=f1;
            f1=num;
            num=temp;
        }
        if(f2<num)
        {
            temp=f2;
            f2=num;
            num=temp;
        }
    }
    System.out.println("First Highest "+f1+" Second Highest "+f2+" Third "+num);

}

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

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