Bitmap Algorithm sorting and searching

Source: Internet
Author: User

Bitmap algorithms are called the simplest algorithms. They can be sorted and searched by a for loop, but they are also the most demanding data, first, the data must be a positive integer, and secondly, the data range should be roughly known and clustered. Bitmap sorting is a sort method with high efficiency (complexity up to O (n) and space saving, however, this sorting method imposes strict requirements on the input data (the data cannot be duplicated and the data range is generally known ). Bitmap sorting uses bitmap or bitvector to represent a set. For example, if there is a set {,}, we can use an 8-bit binary vector set [1-8] to represent the set. If the data exists, set the corresponding binary position 1; otherwise, set it to 0. the set obtained from the given set is {,}, and then the corresponding subscript is output based on the value of the set to obtain the set, the sorting result of 2, 1. This is the principle of Bitmap sorting. 1. Give 4 billion unsigned int integers without sorting out the order, and then give another number. If you can quickly determine whether the number is among those 4 billion. Because the maximum range of unsigned int data is around 4 billion, 40*10 ^ 8/1024*1024*8 = 476, you only need to apply for a memory space of MB. Each bit represents an unsigned int. Read 4 billion, set the corresponding bit to 1, then read the number to be queried, check whether the bit is 1, if it is 1, otherwise it does not exist. 2. How can I determine which of the 4 billion unsigned int integers are duplicated? Similarly, you can apply for 4 billion MB of memory space, read integers, and set the corresponding bit position to 1. If it is the first time to read a data, the bit must be 0 before the bit location 1; if it is the second time to read the data, you can judge whether the data is repeated based on whether the corresponding bit is 1. The spatial complexity of this algorithm lies in the data type. For the sake of convenience, int is actually a Byte or bit! [Java] int [] data = }; // [1-20] because these numbers are between 1 and 20 int [] a = new int [20]; // The default value is all 0 for (int I = 0; I <data. length; I ++) {if (a [data [I]! = 1) {a [data [I] = 1;} else {System. out. println (data [I] + "repeated numbers") ;}} System. out. println ("sorting result"); for (int I = 1; I <= 19; I ++) {if (a [I] = 1) {// output subscript System. out. print (I + "");} www.2cto.com} System. out. println ("\ n10 is in this array? "); System. out. println (a [10] = 1); [java] 4 is the repeated number 9 is the repeated number sorting result 1 4 6 9 10 19 10 is in this array? True

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.