December 16, 2014 17:15:09
Initializes a string of all 0 binary;
An array of unordered integers;
If the integer x is in this integer array, the x position of the binary string is 1;
The binary string is then read sequentially, and the bits of 1 are converted into integers, stored sequentially in the new set, which is ordered.
Sort code:
1 function Sort()2 {3 //var_dump (Php_int_max, php_int_size);4 //int 92233720368547758075 //int 86 $bitmap=Array_fill(0, 50, 0);//apply an array of shapes, 50 elements, initialized to integers 07 $int _bit_size= Php_int_size * 8;//The number of bits for each shaping in the $BITMAP (in this case int = 8*8 = 64bit; $bitmap array altogether 50*64 = 3,200 bit bits), which means that the integer set with a maximum value of less than or equal to 3200 is sorted8 $a=Array(1,4,3,50,34,60,100,88,200,150,300);//define an array of random order9 Ten //Scan each number in $ A to convert it to x*64 + y One foreach($a as $k=$v) { A $shang=$v/$int _bit_size; - $yushu=$v%$int _bit_size; - the $offset= 1 <<$yushu; - - $bitmap[$shang] =$bitmap[$shang] |$offset;//Place the bit position at 1 - } + - //You can get the sorted array by reverting the bit bits in $bitmap to the integer output in turn + $b=Array(); A foreach($bitmap as $k=$v) { at for($i= 0;$i<$int _bit_size;$i++) { - $tmp= 1 <<$i; - $flag=$tmp&$bitmap[$k]; - - //$b [] = $flag? $k * $int _bit_size + $i: false; - if($flag) { in $b[] =$k*$int _bit_size+$i; - } to } + } - the Var_dump($b);Exit; * } $ //Browser output:Panax Notoginseng Array -0 = int 1 the1 = int 3 +2 = int 4 A3 = int 34 the4 = int 50 +5 = int 60 -6 = int 88 $7 = int 100 $8 = int 150 -9 = int 200 -Ten = 300 int
To find the intersection code:
Public function Sort($a=Array()) { //var_dump (Php_int_max, php_int_size); int 9223372036854775807//int 8 $bitmap=Array_fill(0, 50, 0);//apply an array of shapes, 50 elements, initialized to integers 0 $int _bit_size= Php_int_size * 8;//The number of bits per shaping in the $bitmap (in this case int = 8*8 = 64bit; $bitmap array altogether 50*64 = 3,200 bit bits)//$a = Array (1,4,3,50,34,60,100,88,200 , 150,300); Set a disorderly array//scan each number in $ A to convert it to x*64 + y foreach($a as $k=$v) { $shang=$v/$int _bit_size; $yushu=$v%$int _bit_size; $offset= 1 <<$yushu; $bitmap[$shang] =$bitmap[$shang] |$offset;//Place the bit position at 1 } return $bitmap; } Public functionintersect () {$int _bit_size= Php_int_size * 8; $a=Array(1,4,3,50,34,60,100,88,200,150,300); $b=Array(1,5,3,50,34,55,100,87,222,150,300); $bit _a=$this-Sort($a); $bit _b=$this-Sort($b); $c=Array(); foreach($bit _a as $k=$v) { $c[$k] =$bit _a[$k] &$bit _b[$k]; Binary & Compute Intersection}$d=Array(); foreach($c as $k=$v) { for($i= 0;$i<$int _bit_size;$i++) { $tmp= 1 <<$i; $flag=$tmp&$c[$k]; //$b [] = $flag? $k * $int _bit_size + $i: false; if($flag) { $d[] =$k*$int _bit_size+$i; } } } Var_dump($d);Exit; } Browser Output:Array0 = int 1 1 = int 3 2 = int 3 = int 4 = int 5 = int 6 + int 300
Reference:
http://kevinbest0702.blog.163.com/blog/static/85409746201291484128939/
Http://www.cnblogs.com/dolphin0520/archive/2011/10/19/2217369.html
PHP implementation bitmap Bitmap sorting to find the intersection