RPC requests in distributed systems often occur in a disorderly sequence.
Write an algorithm to order a sequence of sequential output. For example , suppose the starting sequence number is 1, for (1, 2, 5, 8, ten, 4, 3, 6, 9, 7) This sequence, the output is :
1
2
3, 4, 5
6
7, 8, 9, 10
??
In the above example, when3 arrives, 4,5 is already there. The entire sequence (3, 4, 5) that already satisfies the order is therefore output to one line.
??
Requirements:
1. write an efficient algorithm to complete the above functions, to achieve as robust as possible, easy to maintain
2. Designing and Implementing unit tests for the algorithm
??
With Bitset
??
Defines a seq array, initialized to 1 for the position except Index=1 , the remaining all zeros, and the length number.length+2
??
Determines if the seq corresponds to a bit 1, if 1, the output, if it is also 1, continues the output
??
After continuous output, the next position is 1, indicating the next number to be output
??
Package Com.alibaba;
??
public class Sol2 {
static void Myprint (int [] Seq,int cur) {
if (seq[cur]!=0) {
System.out.print (cur);
while (seq[++cur]!=0) {
System.out.print (cur);
}
System.out.println ();
}
Seq[cur]=1;
}
public static void Main (string[] args) {
TODO auto-generated Method Stub
int Numbers[]={1, 2, 5, 8, 11, 10, 4, 3, 6, 9, 7};
int [] seq=new int[numbers.length+2];
Seq[1]=1;
for (int i = 0; i < numbers.length; i++) {
Myprint (seq, numbers[i]);
}
}
??
}
??
Ali additional problem distributed RPC request disorderly sequence