寻找第四大&数组中的第四个最小数字

时间:2014-02-01 17:09:10

标签: java arrays sorting

我尝试过以下方法来寻找第二大的&最小的数字,它的工作原理:

class Test

{

static void main()throws IOException

{
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter");
    int a[] = {45,79,2,5,74,4,19,56,2,888};
    int small= a[0];
    int big=a[0];
    int s=0,b=0;
    for(int i=0;i<10;i++)
    {
        if(small>a[i])
        {
            small=a[i];
        }
        if(big<a[i])
        {
            big=a[i];
        }
    }

    int small2=a[0],big2=a[0];

    for(int i=0;i<10;i++)
    {
        if(small2>a[i]&&a[i]!=small)
        {
            small2=a[i];
        }
        if(big2<a[i]&&a[i]!=big)
        {
            big2=a[i];
        }
    }
    System.out.println("second biggest = "+big2);
    System.out.println("second smallest = "+small2);
}
}

现在我想找到第四个最大和最低的。在这种情况下,我可能需要使用4个循环。但我想以更短,更聪明的方式做到这一点。有人可以帮忙吗?

6 个答案:

答案 0 :(得分:3)

这里有一些解决方案:

  1. 如果您不想对整个输入进行排序,可以放置一个您保持排序的临时集合(或使用已经排序的集合)。

    例如,如果输入为“1,2,3,4,5,6,7,8,9,... 1000”,并且您希望获得第m个最大数字,则创建一个大小为m的临时集合,以及你经过的每个数字,你决定它是否应该在临时数组中。你应该始终对临时集合进行排序(或者只使用一个已经排序的集合),并在其中放入一个新项目,同时删除最小的项目,以防它的大小超过m。

    就内存而言,使用小尺寸的集合项(m)并且不修改输入数组。

    就操作数量(复杂性)而言,你得到的排序与排序数组相同 - O(nlogn),因为对于放入临时数组的每个项目,集合需要放置它的位置,并且采用logn(例如使用二进制搜索)。

    顺便说一句,这个解决方案与获取最大/最小数字大致相同,只是你不需要对临时集合中的项目进行排序,因为它的大小为1。

  2. 如果您不关心内存,但只是不想更改输入,则可以复制数组并对其进行排序......

  3. 有一种更好的算法可以在线性时间内工作,其工作方式类似于获得中位数的方法(链接here)。 here's一个更好地展示它的链接。

答案 1 :(得分:2)

您可以使用泛型方法。使用Arrays.sort(array);对数组进行排序,然后您可以使用索引访问它。

答案 2 :(得分:2)

我认为最简单的方法是订购数组(是Log(n))。

如果你不想改变原始数组,你可以先进行克隆(int b [] = Arrays.copyOf(a,a.length);)。

如果你想要数组中的位置而不是值告诉我,我会找到另一种解决方案。

告诉我这是否有帮助:

    public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter");
    int a[] = { 45, 79, 2, 5, 74, 4, 19, 56, 2, 888 };

    Arrays.sort(a);
    System.out.print("ordered array: ");
    for(int i:a){
        System.out.print(i+", ");
    }
    System.out.println();
    System.out.println("the 4th smallest : " + a[3]);
    System.out.println("the 4th biggest: " + a[(a.length - 4)]);

}

答案 3 :(得分:2)

对一般数组进行排序是O(nlogn),然后您可以按索引选择元素。

您还可以使用具有O(n)最坏情况时间复杂度的Select algorithm。算法得到一个数组和一些k,并返回第k个大元素。我相信你可以找到它的实现。

答案 4 :(得分:1)

public class FourthLargest {

    static int a[] = {45, 79, 2, 5, 74, 4, 19, 56, 2, 888};

    public static void main(String[] args) {
        Arrays.sort(a);
        System.out.println("The fourth smallest number = " + a[3]);
        System.out.println("The fourth largest number = " + a[a.length-4]);
    }
}

答案 5 :(得分:1)

为了找到未排序数组的第i个元素,有一个非常简单的基于快速排序(quickselect)的随机算法,采用O(n)平均时间,以及一个非常复杂的非随机算法,采用O(n)最坏情况时间。

您需要的一切都在these powerpoint slides。只是为了提取O(n)最坏情况算法的基本算法:

选择(A,N,I):     将输入分为大小为5的⌈n/5⌉组。

/* Partition on median-of-medians */
medians = array of each group’s median.
pivot = Select(medians, ⌈n/5⌉, ⌈n/10⌉)
Left Array L and Right Array G = partition(A, pivot)

/* Find ith element in L, pivot, or G */
k = |L| + 1
If i = k, return pivot
If i < k, return Select(L, k-1, i)
If i > k, return Select(G, n-k, i-k)

Cormen等人的“算法导论”一书中也详细介绍了

对于最小元素,您只需检查每个元素,并且将花费O(n)时间。

相关问题