Пример #1
0
        public static List <int> SortedMerge(int[][] files)
        {
            BinaryMaxHeap <LineItem> heap = new BinaryMaxHeap <LineItem>();
            List <int> result             = new List <int>();

            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].Length > 0)
                {
                    int value = files[i][0];
                    heap.Push(value, new LineItem(value: value, fileNum: i, nextIndex: 1));
                }
            }

            while (heap.Count > 0)
            {
                LineItem item = heap.Pop();
                result.Add(item.Value);

                int fileNum   = item.FileNum;
                int fileIndex = item.NextIndex;
                if (fileIndex < files[fileNum].Length)
                {
                    int value = files[fileNum][fileIndex];
                    heap.Push(value, new LineItem(value, fileNum, fileIndex + 1));
                }
            }

            return(result);
        }