Package array;
Import Java.util.PriorityQueue;
Import Java.util.Queue; Import sort.
Sort; /** * Known as two ascending arrays A={ai} and B={BJ}, for A[i]+b[j], the first k minimum value * */public class Mintwoarrayk {/** * algorithm complexity (O (n^2)) * 1, first of all possible values Out * 2, sort * @param a incrementing array * @param b incrementing array * @param k before k minimum/public static void SortMethod (int a[], int b[],
int k) {int c[] = new int[a.length * B.length];
for (int i = 0; i < a.length. i++) {for (int j = 0; J < B.length; J +) {C[i * a.length + j] = A[i] + b[j];
} sort.buddlesort (c);
for (int i = 0; i < K; i++) {System.out.print (C[i] + ""); }/** * algorithm complexity K (log (k)) * 1, according to the current node state, get the next possible state * 2, the general algorithm: if the current a[i]+b[j] minimum, the next smallest state may be a[i+1]+b[j] or a[i]+b[j+ 1]. If so, there will be duplicates, such as A[i+1]+b[j] the next state is * a[i+1]+b[j+1] and a[i+2]+b[j], but a[i]+b[j+1 the next state a[i+1]+b[j+1] and a[i]+b[j+2] so a[i+1 ]+B[J+1] will produce duplicates, so * will produce redundant operations * 3, Optimized algorithm:: If the current a[i]+b[j] the smallest, the next state is a[i]+b[j+1], only when i=0, to join the next state A[i+1]+b[j]. Thus achieving the purpose of pruning * each time the possible state * @param A incrementing array * @param b incrementing array * @param k before k minimum */public static void CompareMethod (int a[], int b[], int k) {class N
ODE implements comparable{private int asub = 0;
private int bsub = 0;
private int value = 0;
Public Node (int asub, int bsub, int value) {this.asub = asub;
This.bsub = BSub;
This.value = value;
@Override public int compareTo (Object o) {node node = (node) o;
if (This.value < node.value) return-1;
else return 1;
int count = 0;
int c[] = new Int[k];
queue<node> queue = new priorityqueue<node> ();
Queue.add (New Node (0, 0, a[0] + b[0));
Prevents no K while (Count < K &&!queue.isempty ()) {node node = Queue.poll ();
c[count++] = Node.value;
if (node.bsub + 1 < b.length) {Queue.add (new node (node.asub, Node.bsub + 1, a[node.asub] + b[node.bsub + 1)); } if (node.bsub = = 0 && node.asub + 1 < a.length) {Queue.add (new node (node.asUB + 1, node.bsub, A[node.asub + 1] + b[node.bsub));
for (int i = 0; i < K; i++) {System.out.print (C[i] + ""); }/** * algorithm complexity K (log (k)) * 1, according to the current node state, get the next possible state * 2, the general algorithm: if the current a[i]+b[j] minimum, the next smallest state may be a[i+1]+b[j] or a[i]+b[j+1]. If so, there will be duplicates, such as A[i+1]+b[j] the next state is * a[i+1]+b[j+1] and a[i+2]+b[j], but a[i]+b[j+1 the next state a[i+1]+b[j+1] and a[i]+b[j+2], where there are duplicates
, the idea is to clear duplicate items. * * @param a incrementing array * @param b incrementing array * @param k before k minimum */public static void Comparemethodtwo (int a[], int b[], in
T k) {class Node implements comparable{private int asub = 0;
private int bsub = 0;
private int value = 0;
Public Node (int asub, int bsub, int value) {this.asub = asub;
This.bsub = BSub;
This.value = value;
@Override public int compareTo (Object o) {node node = (node) o;
if (This.value < node.value) return-1;
else return 1;
@Override public boolean equals (Object o) {node node = (node) o; Return This.asub = = Node.asub && this.bsub = = node.bsub;
int count = 0;
int c[] = new Int[k];
queue<node> queue = new priorityqueue<node> ();
Queue.add (New Node (0, 0, a[0] + b[0));
Prevents no K while (Count < K &&!queue.isempty ()) {node node = Queue.poll ();
c[count++] = Node.value;
if (node.bsub + 1 < b.length) {node TMP = new node (node.asub, Node.bsub + 1, a[node.asub] + b[node.bsub + 1]);
if (!queue.contains (TMP)) Queue.add (TMP); } if (Node.asub + 1 < a.length) {node TMP = new node (node.asub + 1, node.bsub, A[node.asub + 1] + b[node.bsub])
;
if (!queue.contains (TMP)) Queue.add (TMP);
for (int i = 0; i < K; i++) {System.out.print (C[i] + "");
} public static void Main (string[] args) {int a[] = {10, 11, 12, 13, 17, 30};
int b[] = {-5,-2, 0, 9, 10, 43};
CompareMethod (A, B, 5);
System.out.println ("");
CompareMethod (A, B, 5); System.out.prinTLN ("");
Comparemethodtwo (A, B, 5);
}
}