简体   繁体   English

这是使用 HeapSort 的 O(nlogn) 的时间复杂度吗

[英]Is this time complexity of O(nlogn) using HeapSort

Is this in time complexity of O(nlogn) time?这是 O(nlogn) 时间的时间复杂度吗? If not how do I fix this如果不是我该如何解决这个问题

Goal: Using HeapSort supposed to find sum pairs using 1 number in each array to find a + b = c (given)目标:使用 HeapSort 应该在每个数组中使用 1 个数字找到和对,以找到 a + b = c(给定)

Basic HeapSort sort function基本的 HeapSort 排序函数

boolean SumPairs(int[] Arr1, int[] Arr2, int p) {

    heapSort(Arr2, p);

    int target = 0;

    for (int i = 0; i < Arr1.length; i++) {
        target = p - Arr1[i];
    if (BinarySearch(Arr2, target) != -1)
        return true;
  }
    return false;
 }

The average time complexity of a correctly implemented heap sort is O(NlogN);正确实现的堆排序的平均时间复杂度是 O(NlogN); see https://en.wikipedia.org/wiki/Heapsort .请参阅https://en.wikipedia.org/wiki/Heapsort

The average time complexity of a correctly implemented binary search is O(logN);正确实现的二分查找的平均时间复杂度是 O(logN); see https://en.wikipedia.org/wiki/Binary_search_algorithmhttps://en.wikipedia.org/wiki/Binary_search_algorithm

So, assuming that the methods you are calling are correctly implemented , the average time complexity of your methods is O(NlogN) + (O(N) * O(logN)) per call.因此,假设您调用的方法已正确实现,您的方法的平均时间复杂度为每次调用O(NlogN) + (O(N) * O(logN)) That reduces to O(NlogN) .这减少到O(NlogN)


Note that this is actually a method with two array parameters, so strictly speaking, the complexity class is: O(NlogN + MlogN) were M is the length of the first array and N is the length of the second one.注意,这实际上是一个有两个数组参数的方法,所以严格来说,复杂度类是: O(NlogN + MlogN)其中M是第一个数组的长度, N是第二个数组的长度。

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

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