взять K / 2 элементов из M и K / 2 элементов из N. Сортировать M-подмножество и N-подмножество.Теперь пересечение тривиально.Запишите пересечение, отбросьте элементы пересечения, запишите левые элементы.Продолжите со всеми другими частями K / 2 M и N. Теперь у вас есть некоторые общие элементы в третьем файле и некоторые частично отсортированные списки.Теперь для каждого K / 2 (за вычетом удаленных элементов) списков M и N сравните их, чтобы эффективно найти пересечение.
(Вы также можете добавить дополнительные раунды слияния 2 M-подмножеств и N-подмножеств в скоростьпересечение вверх).
Ура!
Пример выполнения:
Ok let's take 2 lists of 9 integers with values between 0 and 9.
These lists will be
4 2 5 1 9 2 9 0 2
and
4 7 4 0 8 6 2 6 5
(generated by rand())
So we take two sublists :
4 2 5 and 4 7 4
Let's sort them using quicksort
4 2 5
i j <- two pointers
pivot = [0] = 4
increase i until it's greater than pivot = 4
i and j now points to 5
decrease j until it's smaller than pivot = 4
j points to 2
i is greater than j, thus except for pivot, the list is sorted.
Swap [0] and [j] to finish sorting
2 4 5.
Except for pivot + i + j, no extra allocation was needed (For 3 elements it's hard to believe, see any quicksort applet to have a better understanding).
Thus, from an algorithmic point of view, pivot + i + j are constant and can be neglected (in fact you would do memory - algorithm size to have the real K).
Do the same for the second set
(4 4 7)
Now with two pointers to the lists initialized to the start of the sublists, compare the pointed values.
2 - 4
Increase first sublist's pointers
4 - 4
Equal -> a first common element, remove them from list [2 5] and [4 7]
5 - 4
Increase second pointer
5 - 7
Increase first pointer
End of this sublists.
Dump this to original lists.
Do this for any other sublists
[1 9 2] and [0 8 6] -> [1 2 9] [0 6 8] -> no intersection
[9 0 2] and [2 6 5] -> [0 9 E] [5 6 E] -> [2]
Now we have :
[2 5 E 1 2 9 0 9 E] and [4 7 E 0 6 8 5 6 E] with E for empty elements.
Now compare subsets with other subsets (sorted) (excepted for already processed sets (same index))
[2 5 E] with [0 6 8] and [6 5 E] -> becomes [2 E E] and [0 6 8] [6 E E] (intersection [5])
Then
[1 2 9] with [4 7 E] and [6 E E] -> [1 2 9] and [4 7 E] [6 E E] (no intersection)
Then
[0 9 E] with [4 7 E] and [0 6 8] -> [9 E E] and [4 7 E] [6 8 E] (intersection [0])
End :
[2 E E 1 2 9 9 E E] [4 7 E 6 8 E 6 E E] with common elements [4 2 5 0]