Ali additional problem distributed RPC request disorderly sequence

Source: Internet
Author: User

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

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.