Algorithm-"Black and White inverse" array element deletion method

Source: Internet
Author: User
Algorithm -- & quot; black/white inverse & quot; array element deletion method -- Linux general technology-Linux programming and kernel information. For details, see the following. "Black and White" array element deletion method

2005-10-28 yang_wm CLICK: 137

Recently I used the algorithm to delete array elements when writing a program.

Because the array is large and the previous algorithm is used, the efficiency is very low,

Therefore, we have to think of a better solution to the array element deletion algorithm.

Here is an example of an algorithm for deleting an array element:

For example, an array: int array [100], I;

For (I = 0; I <100; I ++)

Array ;

Now, to delete the elements of the array, the time complexity in the worst case is O (n ^ 2 );

For example, to delete all elements, the time complexity is O (n ^ 2 );

The best case is time complexity: O (n );

The time complexity of deleting an element is O (n );



My improved algorithm ideas:

Search for undeleted elements in sequence, and put them back into the array in sequence.

For example, array a [10] = {1, 2, 3 ...... 10 };

Now I want to delete two elements, a [1] = 2, and a [5] = 6.

Set a [0] = a [0];

A [1] = a [2];

A [2] = a [3];

A [3] = a [4];

A [4] = a [6];

A [5] = a [7];

A [6] = a [8];

A [7] = a [9];

Looking at the left and right array elements above, the left side is continuous (a [0] ...... a [7]), while the right side is the elements except a [1], a [5.

Then we will give two pointers p1 and p2. P2 is responsible for searching for elements not to be deleted, and p1 is responsible for recording the location where the current element is moved,

If p2 finds an element that is not deleted, it will pay the element indicated by p2 to the position indicated by p1.

P1 p2

|

V

A [0]-> a [1]-> a [2]-> a [3]-> a [4]-> a [5]-> a [6] -> a [7]-> a [8]-> a [9]
Regardless of the number of elements to be deleted, the time complexity is O (n) in the worst case ).
"Black and White inversion" means that the elements to be deleted are black, the elements not deleted are white, and the deletion was originally performed on "Black, this algorithm operates on the "white" elements not to be deleted, so it is called the "Black and White inverse" array element deletion algorithm.

Yang Wei
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.