Write an algorithm step by step (to find the missing number)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

 

 

Suppose we have 0.1 billion pieces of data, and the data range is 0 ~ 0.1 billion, that is, m of data. However, some data is lost in this array, for example, 5 or 10 less. So, how can we retrieve the lost data? This question is not difficult, but it can help us expand our thinking and continuously improve the efficiency of algorithm operation.

 

The simplest way to solve this problem is to flag the data and output the data in sequence.

 

 

Copy to clipboardprint? Void get_lost_number (int data [], int length)

{

Int index;

 

Assert (NULL! = Data & 0! = Length );

Unsigned char * pFlag = (unsigned char *) malloc (length * sizeof (unsigned char ));

Memset (pFlag, 0, length * sizeof (unsigned char ));

 

For (index = 0; index <length; index ++ ){

If (0 = pFlag [data [index])

PFlag [data [index] = 1;

}

 

For (index = 0; index <length; index ++ ){

If (0 = pFlag [index])

Printf ("% d \ n", index );

}

 

Free (pFlag );

Return;

}

Void get_lost_number (int data [], int length)

{

Int index;

 

Assert (NULL! = Data & 0! = Length );

Unsigned char * pFlag = (unsigned char *) malloc (length * sizeof (unsigned char ));

Memset (pFlag, 0, length * sizeof (unsigned char ));

 

For (index = 0; index <length; index ++ ){

If (0 = pFlag [data [index])

PFlag [data [index] = 1;

}

 

For (index = 0; index <length; index ++ ){

If (0 = pFlag [index])

Printf ("% d \ n", index );

}

 

Free (pFlag );

Return;

} A Friend may also see that the above code needs to allocate the same length space as the original data. In fact, we can use bit to set the access flag, so the space we apply for can be reduced.

 

 

Copy to clipboardprint? Void get_lost_number (int data [], int length)

{

Int index;

Assert (NULL! = Data & 0! = Length );

Unsigned char * pFlag = (unsigned char *) malloc (length + 7)> 3 );

Memset (pFlag, 0, length * sizeof (unsigned char ));

For (index = 0; index <length; index ++ ){

If (0 = (pFlag [data [index]> 3] & (1 <(data [index] % 8 ))))

PFlag [data [index]> 3] | = 1 <(data [index] % 8 );

}

For (index = 0; index <length; index ++ ){

If (0 = (pFlag [data [index]> 3] & (1 <(data [index] % 8 ))))

Printf ("% d \ n", index );

}

Free (pFlag );

Return;

}

Void get_lost_number (int data [], int length)

{

Int index;

Assert (NULL! = Data & 0! = Length );

Unsigned char * pFlag = (unsigned char *) malloc (length + 7)> 3 );

Memset (pFlag, 0, length * sizeof (unsigned char ));

For (index = 0; index <length; index ++ ){

If (0 = (pFlag [data [index]> 3] & (1 <(data [index] % 8 ))))

PFlag [data [index]> 3] | = 1 <(data [index] % 8 );

}

For (index = 0; index <length; index ++ ){

If (0 = (pFlag [data [index]> 3] & (1 <(data [index] % 8 ))))

Printf ("% d \ n", index );

}

Free (pFlag );

Return;

} The code above has been reduced in space. So how can we calculate the data in parallel?

Copy to clipboardprint? Void get_lost_number (int data [], int length)

{

Int index;

RANGE range [4] = {0 };

Assert (NULL! = Data & 0! = Length );

Unsigned char * pFlag = (unsigned char *) malloc (length + 7)> 3 );

Memset (pFlag, 0, length * sizeof (unsigned char ));

 

Range [0]. start = 0, range [0]. end = length> 2;

Range [1]. start = length> 2, range [1]. end = length> 1;

Range [2]. start = length> 1, range [2]. end = length> 2*3;

Range [3]. start = length> 2*3, range [3]. end = length;

 

# Pragma omp parallel

For (index = 0; index <4; index ++ ){

_ Get_lost_number (data, range [index]. start, range [index]. end, pFlag );

}

For (index = 0; index <length; index ++ ){

If (0 = (pFlag [data [index]> 3] & (1 <(data [index] % 8 ))))

Printf ("% d \ n", index );

}

Free (pFlag );

Return;

}

Void get_lost_number (int data [], int length)

{

Int index;

RANGE range [4] = {0 };

Assert (NULL! = Data & 0! = Length );

Unsigned char * pFlag = (unsigned char *) malloc (length + 7)> 3 );

Memset (pFlag, 0, length * sizeof (unsigned char ));

 

Range [0]. start = 0, range [0]. end = length> 2;

Range [1]. start = length> 2, range [1]. end = length> 1;

Range [2]. start = length> 1, range [2]. end = length> 2*3;

Range [3]. start = length> 2*3, range [3]. end = length;

 

# Pragma omp parallel

For (index = 0; index <4; index ++ ){

_ Get_lost_number (data, range [index]. start, range [index]. end, pFlag );

}

For (index = 0; index <length; index ++ ){

If (0 = (pFlag [data [index]> 3] & (1 <(data [index] % 8 ))))

Printf ("% d \ n", index );

}

Free (pFlag );

Return;

} For multi-core parallel computing, we have added the subfunction _ get_lost, which is further supplemented.

 

 

Copy to clipboardprint? Typedef struct _ RANGE

{

Int start;

Int end;

} RANGE;

 

Void _ get_lost_number (int data [], int start, int end, unsigned char pFlag [])

{

Int index;

 

For (index = start; index <end; index ++ ){

If (0 = (pFlag [data [index]> 3] & (1 <(data [index] % 8 ))))

PFlag [data [index]> 3] | = 1 <(data [index] % 8 );

}

}

Typedef struct _ RANGE

{

Int start;

Int end;

} RANGE;

 

Void _ get_lost_number (int data [], int start, int end, unsigned char pFlag [])

{

Int index;

 

For (index = start; index <end; index ++ ){

If (0 = (pFlag [data [index]> 3] & (1 <(data [index] % 8 ))))

PFlag [data [index]> 3] | = 1 <(data [index] % 8 );

}

}

Summary:

 

(1) code optimization can be performed continuously, but not applicable to all scenarios.

 

(2) At present, the cpu has started to change from 2-> 4-> 8-core. If possible, friends should learn as much as possible about multi-core programming.

Related Article

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.