简体   繁体   English

排序算法解释复杂性和类型

[英]Sorting Algorithm Explanation Complexity and Type

This is a sorting algorithm in Java.这是Java中的排序算法。 I have to analyze this algorithm but I'm really new to the subject and not sure about what each line does.我必须分析这个算法,但我对这个主题真的很陌生,不确定每一行的作用。 I know that it's of the complexity of O(N²) but I don't understand what type it is.我知道它具有O(N²)的复杂性,但我不明白它是什么类型。
Could someone please explain the method someAlgorithm() ?有人可以解释一下方法someAlgorithm()吗?

This is the code:这是代码:

import java.util.Arrays;

public class SomeClass {
    public static void main(String[ ] args) {
        int[] data = {12, 14, 13, 15, 19, 17, 16, 11, 18, 20};
        System.out.println("Compute some result for the Array : " + Arrays.toString(data));
        int[] result = someAlgorithm(data);
        System.out.println("Resulting Array: " + Arrays.toString(result));
    }
 
    private static int[] someAlgorithm(int[] data) {
        int size = data.length;
        int[] result = new int[size];
        for (int i = 0; i < size; i++) {
            int total = 0;
            for (int j = 0; j <= i; j++) {
                total += data[j];
            }
            int average = total / (i + 1);
            result[i] = average;
            System.out.println("Average for iteration " +  i + " is: " + average);
        }
        return result;
    }
}

The gist of the method someAlgorithm() is that it takes an array data as input and creates another array result of the same size, where result[i] stores the average (as an int ) of the first i elements of the input array.方法someAlgorithm()的要点是它将数组data作为输入并创建另一个相同大小的数组result ,其中result[i]存储输入数组的前i个元素的平均值(作为int )。

Note that you can keep a running sum of the elements seen so far and reduce the complexity of this algorithm to O(N) fairly easily.请注意,您可以保持到目前为止看到的元素的运行总和,并将该算法的复杂性相当容易地降低到O(N)

Also, as @Alex mentioned in the comments, this really has nothing to do with sorting and is not a type of sorting algorithm.此外,正如评论中提到的@Alex,这实际上与排序无关,也不是一种排序算法。

There are two types of algorithmic complexity :算法复杂度有两种类型:

  1. Space Complexity空间复杂度
  2. Time Complexity时间复杂度

As you said, this algorithm runs in O(N²) , and this is in regards to time complexity .正如你所说,这个算法在O(N²)中运行,这是关于时间复杂度的。 This is because the time the algorithm will take to run increases with the square of the size variable.这是因为算法运行的时间随着size变量的平方而增加。 As for space complexity , it runs in O(N) , as the space (that is, the amount of memory) it takes up increases linearly with the size variable.至于空间复杂度,它运行在O(N)中,因为它占用的空间(即内存量)随着size变量线性增加。

To see which time complexity an algorithm runs in, you need to look at how many nested loops there are, and how long they run for depending on the input.要查看算法运行的时间复杂度,您需要查看有多少嵌套循环,以及它们运行多长时间,具体取决于输入。

To see which space complexity an algorithm runs in, you need to look at when and where data is stored, and how much of it is stored depending on the input.要查看算法运行的空间复杂度,您需要查看数据存储的时间和位置,以及根据输入存储的数据量。

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

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