【问题标题】:Java find common elements in a collectionJava 查找集合中的公共元素
【发布时间】:2014-03-19 14:48:17
【问题描述】:

我需要在数组集合中找到共同的元素

public Comparable[] findCommonElements(Object[] collection)

作为我的算法的签名,它应该接受一组数组(长度可变和任何类型)作为输入,并且不大于 O(knlogn)。

我想对数组进行快速排序 (??),然后对公共元素进行二进制搜索。这应该使我处于 O(knlogn),但我不能 100% 确定效率。

我不知道如何让二进制搜索来搜索集合,然后打印公共元素。我知道我不能从静态方法中调用 common,但我离开它是为了了解我尝试过的内容。我知道我的时间最好花在学习如何使用数组列表和哈希集上,但我应该使用已经涵盖的概念,而这些还没有。

代码:

public class CommonElements2<T extends Comparable<T>>
{
   Comparable[] tempArr;
   Comparable[] queryArray;
   Comparable[] common = new Comparable[queryArray.length];
   int counter = 0;
/* 
sort algorithm goes here
*/   
   public Comparable[] findCommonElements(Object[] collections)
   {
      queryArray = ((Comparable[])collections[0]);
      boolean found = false;

      for(int x = 0; x < queryArray.length; ++x)
      {
         for(int y = 1; y < collections.length; ++y)
         {
            tempArr = (Comparable[])collections[y];
            found = binarySearch(tempArr, 0, tempArr.length, queryArray[x]);

            if(!found)
            {
               break;
            }
            if(found)
            {
               common[counter] = queryArray[x];
               ++counter;
            }  
         } //end y for loop
      } // end x for loop
      return common;      
   } // end findCommonElements

   public boolean binarySearch(Comparable[] arr, int first, int last, Object searchItem)
   {
      boolean found;
      int mid = (first + (last - first)) /2;

      if(first > last)
         return false;

      int value = ((Comparable)searchItem).compareTo(arr[mid]);

      if(value < 0)
         value = -1;

      switch(value)
      {
         case 0:
            found = true;
            break;
         case -1:
            found = binarySearch(arr, first, mid - 1, searchItem);
            break;
         default:
            found = binarySearch(arr, mid + 1, last, searchItem);
            break;
      }
      return found;
   } //end bianry search

   public static void main(String[] args)
   {
        Object [] collections = new Object[4];
        collections[0] = new Integer[]{3, 4, 9, 8, 12, 15, 7, 13};
    collections[1] = new Integer[]{15,24,50,12,3,9};
    collections[2] = new Integer[]{78,65,24,13,9,3,12};
    collections[3] = new Integer[]{15,78,14,3,2,9,44,12};

        CommonElements2<Integer> one = new CommonElements2<Integer>();
        System.out.println(one.findCommonElements(collections));

   }
} // end class

感谢您的帮助和意见!

【问题讨论】:

  • 该方法声明来自您还是直接来自您的分配?
  • 作业需要它
  • 我的意思是如果您的老师/教授将public Comparable[] findCommonElements(Object[] collection) 定义为方法签名,或者这是您的想法。
  • 您假设数组已经排序。如果不是,那么您可以使用已经有 O(nlogn) 时间的Collections#sort 对其进行排序。然后,由于元素已经排序,您可以通过成对比较元素来遍历数组并找出其中的公共元素(从而节省二进制搜索所消耗的时间)。
  • 如果您没有任何内存限制,那么您可以将每个数组的每个元素存储在一个大数组中,然后对其进行排序(时间为 O(knlogn))并执行通过比较彼此相邻的元素进行搜索。

标签: java arrays algorithm collections


【解决方案1】:

根据我的评论,这是一个可以完成您的工作的算法:

  1. 对所有数组分别排序 O(knlogn)。
  2. 从数组数组中取出两个数组 A1 和 A2。
  3. 浏览 A1 的每个项目,并使用二分搜索 O(nlogn) 搜索元素是否在 A2 中。
  4. 将 A1 和 A2 中的公共元素存储到数组 B 中。
  5. 从数组的数组中取出另一个数组作为A2,使用数组B作为A1。
  6. 转到第 3 步并重复该过程,直到您使用了数组 O(knlogn) 中的所有数组。

这是所提议算法的伪代码(看起来像 Java 代码,但 根本不是 Java 代码):

public Comparable[] findCommonElements(Object[] collections) {
    //1.
    for each collection in collections
        Comparable[] compCollection = (Comparable[])collection
        sort(compCollection)
    end for
    //2.
    Comparable[] a1 = (Comparable[])collections[0]
    //assume MAX is a really high value like 10000
    //the best value for MAX would be the max length of the arrays in collections
    Comparable[] b = new Comparable[MAX]
    int bSize = 0
    //6.
    for i = 1 to collections.length - 1
        //5.
        Comparable[] a2 = (Comparable[])collections[i]
        //3.
        for each Comparable comp in a1
            int index = binarySearch(comp, a2)
            if index >= 0 then
                //4.
                add a2[index] into b
                bSize = bSize + 1
            end if
        end for
        //5.
        a1 = b
        b = new Comparable[MAX]
        bSize = 0
    end for
    return b
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2013-09-22
    • 2019-05-03
    • 2016-10-11
    • 2021-10-22
    • 2017-12-23
    • 1970-01-01
    相关资源
    最近更新 更多