Generate all possible sorted arrays from alternate elements of both given sorted arrays

Source: Internet
Author: User
Tags addall

http://www.geeksforgeeks.org/generate-all-possible-sorted-arrays-from-alternate-elements-of-two-given-arrays/

Given-sorted arrays A and B, generate all possible arrays such the first element is the taken from A then from B then fro M A and so on increasing order till the arrays exhausted. The generated arrays should end with a element from B.

For Example

A = {Ten, 20, 30} B = {1, 5;

The resulting arrays is:

10 20

10 20 25 30

10 30

15 20

15 20 25 30

15 30

25 30

Idea: If you only need to output the longest alternating and orderly array, you can use a method similar to MergeSort, but because of the need to output all possible arrays, this is a combinatorial problem, so it is natural to think of a recursive approach to solve, The key to solving the problem with recursion is to analyze the relationship between the problem solution and sub-problems: so we assume that funtion alternatesort (A, Astart, B, bstart, min) can output all A from index Astart, b starting from bstart, the minimum value is greater than the alternating array of min, so we can analyze the situation when astart=i, Bstart=j, Min is min:

A:A1, A2, ..., Ai, ..., AL, Al+1,..., am; (The length of array A is m)

B:B1, B2, ..., BJ, ..., BT, bt+1,..., bn; (The length of array b is n)

Step 1: From AI start to find the first element greater than min; suppose it is Al;

Step 2: Since the elements in a and the elements in B need to be alternately and appear in order from small to large, the element to be found in B must be greater than AL, which is assumed to be BT;

The output of step 3:alternatesort (A, I, B, J, Min) includes:

Al, Bo;

Al, Bo + alternatesort (A, l+1, B, O+1, Bo); (t <=o <= N)

Alternatesort (A, l+1, B, t+1, Min);

AP, BT;

AP, BT + alternatesort (A, p+1, B, t+1, BT); (Al < ap < BT)

Conditions for recursive termination:

Astart >= A.length | | Bstart >= b.length;

Starting from Astart, there is no larger number in the array a than min;

Starting from bstart, there is no larger number than AL in array B;

So the Java code that implements the above algorithm is as follows:

public class solution{

public void Alternatesort (int[] A, int[] B) {

if ((A.length = = 0) | | (B.length = = 0)) {
Return
}

arraylist<arraylist<integer>> ret = new arraylist<arraylist<integer>> ();
ret = Alternatesort (A, 0, B, 0, Integer.min_value);
Printresult (ret);
}

Private arraylist<arraylist<integer>> Alternatesort (int[] A, int astart, int[] B, int bstart, int min) {
arraylist<arraylist<integer>> ret = new arraylist<arraylist<integer>> ();
if ((Astart >= a.length) | | (bstart >= b.length)) {
return ret;
}
int I=astart;
while ((I < a.length) && (A[i] < min)) {
i++;
}
if (i >= a.length) {
return ret;
}
int j = bstart;
while ((J < B.length) && (B[j] < a[i])) {
j + +;
}
if (J >= b.length) {
return ret;
}

{Ai, Bt} t >= J
{Ai, BT} + Alternatesort (A, i+1, B, t+1, BT);
for (int t = j; t < B.length; t++) {
arraylist<integer> s = new arraylist<integer> ();
S.add (A[i]);
S.add (B[t]);
Ret.add (s);

arraylist<arraylist<integer>> suffix = alternatesort (A, i+1, B, T+1, b[t]);
for (arraylist<integer> R:suffix) {
arraylist<integer> ss = new arraylist<integer> ();
Ss.add (A[i]);
Ss.add (B[t]);
Ss.addall (R);
Ret.add (ss);
}
}

{al, bj} Al < BJ
{Al, Bj} + alternatesort (A, l+1, B, J+1, B[j]);
int L = i+1;
while ((L < a.length) && (A[l] < b[j])) {
arraylist<integer> s = new arraylist<integer> ();
S.add (A[l]);
S.add (B[j]);
Ret.add (s);

arraylist<arraylist<integer>> suffix = alternatesort (A, l+1, B, J+1, B[j]);
for (arraylist<integer> R:suffix) {
arraylist<integer> ss = new arraylist<integer> ();
Ss.add (A[l]);
Ss.add (B[j]);
Ss.addall (R);
Ret.add (ss);
}

l++;
}

Alternatesort (A, i+1, B, j+1, Min);
Ret.addall (Alternatesort (A, i+1, B, j+1, Min));

return ret;
}

private void Printresult (arraylist<arraylist<integer>> result) {
for (arraylist<integer> S:result) {
System.out.print ("{");
for (int v:s) {
System.out.print (v);
System.out.print (",");
}
System.out.println ("}");
}
}

}

Generate all possible sorted arrays from alternate elements of both given sorted arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.