Bitmap method definition
Bitmap method is bitmap abbreviation, so-called bitmap, is to use each bit to store a certain state, applicable to large-scale data, but the data state is not a lot of situations. It is often used to determine that a data store does not exist.
For example, to determine the status of 10 million people, each person has only two states: men, women, can be expressed in 0,1. Then you can open an int array, an int has 32 bits, it can represent 32 people. You can use bit operations when you are working. This article address: http://www.cnblogs.com/archimedes/p/bitmap.html, reprint please indicate source address. Data structure unsigned int bit[n]; In this array, you can store n * sizeof (int) * 8 data, but the largest number can only be n * sizeof (int) * 8-1. If we want to store 0-15 of the data, we just need to make n=1, so we can put the data in. For example, if the data is "5,1,7,15,0,4,6,10", then the situation in this structure is used as bitmap method.
one, to 4 billion non-repeating integers of unsigned int, not ordered, and then give a number, how to quickly determine whether the number in the 4 billion numbers
Request 512M of memory
A bit bit represents a unsigned int value
Read 4 billion numbers, set the appropriate bit bit
Read the number of queries to see if the corresponding bit bit is 1, 1 is present, and 0 indicates that it does not exist
second, using bitmap method to determine whether the array is duplicated
It is one of the common programming tasks to judge that there is duplication in a collection, and when the amount of data in the collection is relatively large, we usually want to do fewer scans, when the double-loop method is not available. The bitmap method is suitable for this case, it is done by the largest element in the collection of Max to create a new array of length max+1, and then scan the original array again, encountered a few to the new array of the first position of 1, if encountered 5 to the new array of the sixth element 1, So the next time you encounter 5 want to set the new array to find the sixth element is already 1, which indicates that the data and the previous data must be duplicated. This method of initializing the new array with 0 is similar to the bitmap method, so it is called a bitmap approach. It has a worst-case operation of 2N. If the maximum value of the known quantity group can be fixed in advance for the new array, efficiency can be increased by one more time.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h>bool Hasduplicateditem (int *a, int len) { int length, max, I; length = Len; max = a[0]; for (i = 1; i < length; i++) { if (A[i] > Max) max = A[i]; } int *arr; arr = (int*) malloc (sizeof (int) * (max + 1)); for (i = 0; i < length; i++) { if (Arr[a[i]]) return true; else arr[a[i]] = 1; } return false;} int main () { int length; int test[] = {0,1,2,3,45,12,13}; Length = (sizeof (test)/sizeof (test[0])); if (Hasduplicateditem (test, length)) printf ("hasduplicateditem!\n"); else printf ("hasnoduplicateditem!\n"); return 0;}
third, using the bitmap method for shaping array sorting
First, iterate over the array, get the maximum minimum value of the array, and then narrow the range of bitmap based on this maximum minimum value. It is important to note that the negative numbers for int are converted to unsigned int for processing, and the number is subtracted from the minimum value when the bit is taken.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h>void bitmapsort (int *a, int len) {int length, max, Min, I, index; length = Len; min = max = a[0]; Find the array maximum for (i = 1; i < length; i++) {if (A[i] > max) {max = a[i]; } if (min > A[i]) {min = a[i]; }}//get bitmap array int *arr; arr = (int*) malloc (sizeof (int) * (max-min + 1)); for (i = 0; i < length; i++) {index = a[i]-min; arr[index]++; }//re-arr_length the element int in A; Arr_length = max-min + 1; index = 0; for (i = 0; i < arr_length; i++) {while (Arr[i] > 0) {A[index] = i + min; index++; arr[i]--; }}}void Print (int *a, int n) {int i; for (i = 0; i < n; i++) {printf ("%d", a[i]); } printf ("\ n");} int main () {int length; int test[] = {50,1,26,3,45,12,13}; Length = sizeof (test)/sizeof (test[0]); Print (test, length); Bitmapsort (test, length); Print (test, length); return 0;}
four, the bitmap method to save data
Input: A file containing up to n positive integers, each of which is less than N, where there is no duplicate integer in the n=10,000,000 input file, and no other data is associated with the integer.
Output: These numbers are sorted in ascending order.
Constraint: There is more than 1MB (not more than 2MB) of memory space available, there is plenty of hard disk space.
#include <stdio.h> #define Bitsperword 32#define SHIFT 5#define MASK 0x1f#define N 10000000int a[1 + n/bitsperword];/ * A[i>>shift] is the i-bit should be on the first number of int *//* (1<< (i & MASK)) is the first bit on the int of the first few bit */void set (int i) { a[i>> SHIFT] |= (1<< (i & MASK));} void clr (int i) { a[i>>shift] &= ~ (1<< (i & MASK));} int test (int i) { return A[i>>shift] & (1<< (i & MASK));} int main () { int i; for (i = 0; i < N; i++) CLR (i); while (scanf ("%d", &i)! = EOF) set (i); for (i = 0; i < N; i++) if (test (i)) printf ("%d\n", I); return 0;}
Bitmap Bitmap method