简体   繁体   English

以线性(O(N))时间对数组排序

[英]Sorting an Array in Linear (O(N)) Time

I'm working on an exercise currently regarding Sorting Algorithms. 我正在从事有关排序算法的练习。 The actual exercise is structured like this: 实际练习的结构如下:

Given a sequence of N real numbers, find the pair of integers that are farthest apart in value. 给定一个由N个实数组成的序列,请找到一对值相距最远的整数。 Give a O(N) algorithm. 给出一个O(N)算法。 Describe your proposed algorithm in English. 用英语描述您提出的算法。

I am not familiar currently with any linear sorting algorithms, so I wanted to ask here about it. 我目前不熟悉任何线性排序算法,所以我想在这里问一下。 Is there a specific type of sorting algorithm that would work best in this situation? 是否有特定类型的排序算法在这种情况下最有效? If so, what is it and how does that particular algorithm work? 如果是这样,那是什么,该特定算法如何工作?

Thanks! 谢谢!

There's no need for you to sort. 无需您排序。 All you are doing is finding the min of the list of numbers, which takes O(N), then find the max of the list of numbers, which again takes O(N). 您要做的就是找到数字列表的最小值,取O(N),然后找到数字列表的最大值,再次取O(N)。

Sorting all elements in a list is impossible in O(n) time. 在O(n)时间内不可能对列表中的所有元素进行排序。 It will take minimally O(n log(n)) time. 最少需要O(n log(n))时间。

however, this problem does not require sorting all the elements, only two of them. 但是,此问题不需要对所有元素进行排序,只需对其中两个元素进行排序。

Simply iterate over the list once, and keep the largest and smallest values. 只需遍历列表一次,并保留最大值和最小值。

As being pointed out, you just need to find min and max. 如所指出的,您只需要找到最小值和最大值。 Obviously you can find them separately and it takes O(n), to be exact it takes 2*n comparisons. 显然,您可以分别找到它们,并且需要O(n),确切地说,需要2 * n个比较。 But as in CLRS you can do it better, reducing number of comparisons to 1.5*n by finding min, max concurrently. 但是,就像在CLRS中一样,您可以做得更好,通过同时找到min,max将比较数减少到1.5 * n。 The idea is as follow: 想法如下:

1) initialize min = a[0], max = a[0] 1)初始化最小值= a [0],最大值= a [0]

2) for i=1 to n-1, compare a[i] and a[i+1]. 2)对于i = 1到n-1,比较a [i]和a [i + 1]。 And then compare and update accordingly: min(a[i],a[i+1]) with min; 然后进行相应的比较和更新:min(a [i],a [i + 1])与min; max(a[i],a[i+1]) with max. max(a [i],a [i + 1]),最大

This is what you need : 这就是您需要的:

public class MinMax {
    public static void main(String[] args) {
        int numberarray[] = { 5, 6, 8, 1, 3, 5, 4, 6, 9, 4, 2, 3, 4, 7, 9, 6 };
        int min, max;
        min = max = numberarray[0];

        for (int i = 0; i < numberarray.length; i++) {
            if (numberarray[i] < min)
                min = numberarray[i];
            if (numberarray[i] > max)
                max = numberarray[i];
        }       
        System.out.println("The pair farthest apart is ( "+max+" , "+min+" )");
    }
}

I'm writing in Java. 我正在用Java编写。

int min=Integer.MAX_VALUE; int min =整数.MAX_VALUE; int max=Integer.MIN_VALUE; int max =整数.MIN_VALUE;

for(int i=0;i for(int i = 0; i

   if(a[i]<a[i+1]){ 
      if(a[i]<min) min=a[i];
      if(a[i+1]>max)max=a[i+1];
   }

   else{//a[i]>=a[i+1]
      if(a[i+1]<min) min=a[i+1];
      if(a[i]>max)max=a[i];

    }

} }

So total number of comparisons is 1.5*n; 因此,比较总数为1.5 * n;

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

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