Java 将多个预先排序的ArrayList合并到一个新数组中

Java 将多个预先排序的ArrayList合并到一个新数组中,java,algorithm,sorting,merge,heap,Java,Algorithm,Sorting,Merge,Heap,我在12个文件中有12个预先排序的整数[]数组。我希望将它们合并在一起,就像在合并排序中一样,不同的是,在排序之前,我不能将它们合并到一个数组中。我必须把它们分开,它们的尺寸不能超过50 要求如下: 使用合并技术对各个块进行相互排序,并将输出写入名为“result_using_merge.txt”的文件 使用名为“potentialMinimums”的整数基元数组保存当前最小值,并在排序的每次迭代中打印它 你建议我怎么做 已解决: import java.io.FileNotFoundExcep

我在12个文件中有12个预先排序的整数[]数组。我希望将它们合并在一起,就像在合并排序中一样,不同的是,在排序之前,我不能将它们合并到一个数组中。我必须把它们分开,它们的尺寸不能超过50

要求如下: 使用合并技术对各个块进行相互排序,并将输出写入名为“result_using_merge.txt”的文件

使用名为“potentialMinimums”的整数基元数组保存当前最小值,并在排序的每次迭代中打印它

你建议我怎么做

已解决:

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;

public class BasicArrayMerger
{
   private static ArrayList<Integer> finalCollection = new ArrayList<Integer>();
   private static ArrayList<Integer> minCollection;
   private static int[] minIndex =
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
   private static int[] maxIndex =
   { 49, 49, 27, 49, 49, 27, 49, 49, 27, 49, 49, 27 };
   private static Integer[] minimums;
   private static ArrayList<Integer[]> inputs;
   static PrintWriter outputStream, runStream;


   static void mergeSortedArrays(final int memorySize,
         ArrayList<Integer[]> inputChunks, Integer[] potentialMinimums,
         String outfile)
   {
      try {
          outputStream= new PrintWriter(outfile);
          runStream = new PrintWriter("resources/RUN_merge.txt");
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      }
      inputs = new ArrayList<Integer[]>(inputChunks);
      int totalInts = 512;
      int processedInts = 0;
      while (processedInts < totalInts)
      {
         minCollection = new ArrayList<Integer>();
         minimums = potentialMinimums;

         while (minCollection.size() < memorySize && processedInts < totalInts) 
         {
            for (int i = 0; i < inputs.size(); i++) 
            {
               if (minIndex[i] > maxIndex[i])
                  minimums[i] = 9999;
               else
                  minimums[i] = inputs.get(i)[minIndex[i]];
            }

            Integer value = findMin(minimums);

            if (processedInts%10==0)
            {
               runStream.println("\n\n******"+processedInts+" iteration:");
               runStream.println(Arrays.toString(minimums));
            }

            int valueIndex = findMinIndex(minimums);
            if (value == 9999)
               break;
            else
               minCollection.add(value);
            minIndex[valueIndex] += 1;
            processedInts++;
         }

         outputStream.println(minCollection);
         finalCollection.addAll(minCollection);
      }
      outputStream.close();
      runStream.close();
   }

   public static int findMin(Integer[] minimums)
   {
      if (minimums.length == 0)
         return -1;
      int small = minimums[0];
      int index = 0;
      for (int i = 0; i < minimums.length; i++)
         if (minimums[i] < small) {
            small = minimums[i];
            index = i;
         }
      return small;
   }

   public static int findMinIndex(Integer[] minimums)
   {
      if (minimums.length == 0)
         return -1;
      int small = minimums[0];
      int index = 0;
      for (int i = 0; i < minimums.length; i++)
         if (minimums[i] < small) {
            small = minimums[i];
            index = i;
         }
      return index;
   }

}
import java.io.FileNotFoundException;
导入java.io.PrintWriter;
导入java.util.ArrayList;
导入java.util.array;
公共类基本合并
{
私有静态ArrayList finalCollection=new ArrayList();
私有静态数组列表集合;
私有静态int[]minIndex=
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
私有静态int[]maxIndex=
{ 49, 49, 27, 49, 49, 27, 49, 49, 27, 49, 49, 27 };
私有静态整数[]最小值;
私有静态ArrayList输入;
静态PrintWriter输出流、运行流;
静态无效数据线(最终整数内存化,
ArrayList输入块,整数[]位最小值,
管柱(输出管柱)
{
试一试{
outputStream=新的PrintWriter(输出文件);
runStream=newprintwriter(“resources/RUN_merge.txt”);
}catch(filenotfounde异常){
e、 printStackTrace();
}
输入=新的ArrayList(inputChunks);
整数totalInts=512;
int processedInts=0;
while(processedIntsmaxIndex[i])
最小值[i]=9999;
其他的
最小值[i]=输入。获取(i)[minIndex[i]];
}
整数值=findMin(最小值);
如果(处理数据%10==0)
{
runStream.println(“\n\n*******”+processedits+“迭代:”);
println(数组.toString(最小值));
}
int valueIndex=findMinIndex(最小值);
如果(值==9999)
打破
其他的
minCollection.add(value);
minIndex[valueIndex]+=1;
processedInts++;
}
println(minCollection);
finalCollection.addAll(minCollection);
}
outputStream.close();
runStream.close();
}
公共静态int findMin(最小整数[])
{
如果(最小长度==0)
返回-1;
int small=最小值[0];
int指数=0;
对于(int i=0;i
是的,这是一个家庭作业。@ukaszJuraszek您需要展示您到目前为止所做的工作吗?给我们看一些代码。@ ukasZuraseZek-Max指数[]是硬编码的,Max索引是否应该基于实际文件大小?考虑使用一个席席大小12.你能详细说明吗?