Array copy, array function, stack and queue for simulating data structure by array function, meaning of callback, sorting problem of array function, algorithm and screening method of searching prime number

Source: Internet
Author: User
Tags what php

1. Copying of arrays
Pointer-pointing problem when copying an array.

When the array is copied, the position of the pointer is copied "This copy is exactly the same"
However, if the pointer to the copied array is illegal, the position where the new pointer is copied is initialized.
<?php
$arr 1=array (' 123 ');
End ($arr 1);
Next ($arr 1);//this pointer is illegal
$arr 2 = $arr 1;//here to copy the array
Var_dump (current ($arr 2));//Get a pointer to the ' 123 ' element
Var_dump (current ($arr 1));//This array has a problem with the pointer
?>


But the copy operation itself cannot say:
The essence is, after copying, which array performs the current operation first, which array initializes the "focus"
"When the value is passed, PHP takes a cow" copy on write "mechanism. When the value is passed, PHP is actually limited to a reference point, and only two points to the first change in the amount will point to a newly created value "
This is an optimization idea for PHP: it does not immediately open up a value space for a new variable, but rather a temporary reference to the previous amount
<?php
$arr 1=array (' 123 ', ' 234 ');
End ($arr 1);
Next ($arr 1);//this pointer is illegal
$arr 2 = $arr 1;//here to copy the array
Var_dump (current ($arr 1))//Get a pointer to the ' 123 ' element "Here's the question."
Var_dump (current ($arr 2));//This array has a problem with the pointer
?>

Focus
Similarly: In foreach, after the operation of the original array occurs, because it is necessary to create a copy array "The mechanism used is also a write-time operation Cow"
<?php
$arr 1=array (' bu ', ' Zhao Yun ', ' Dian Wei ');
foreach ($arr 1 as $key = = $value) {
Var_dump ($key, $value);
if ($key = = 2) {
$arr 1[] = ' hero ';//This write operation causes the copy mechanism of foreach
}
Echo ' <br> ';
}
Var_dump (current ($arr 1));//Zhao Yun "because a write operation causes foreach to open the copy mechanism, and the write operation after copy also cow the principle, which causes the $ARR1 pointer to rest in that position, and the foreach traversal is $ Copy array for arr1 "
?>

As long as the array pointer exceeds the expected value, as long as the pointer is searched once, then the pointer points to the first element "pointer initialization"

Focus
foreach vs. pointer issues:
(1) A copy is traversed instead of the original array
(2) The actual operation is only used when the original array has a write operation. "You need to consider array pointer issues in conjunction with the pointer movement of foreach and changes to the pointer"

2. Array functions
Combining manuals for array research "at least you know there's a function that doesn't need to be too deep into the details of the parameters."


3. Using functions to simulate data structures
Simulation stacks and queues "handle a column of data: The stack is advanced and out (equivalent to the last element that pops up). Queue FIFO (processing of the front element) "
Push, Pop/shift, unshift
This will enable the simulation of the list

After the stack and the stack operation, the subscript of the elements of the array is re-indexed "and when elements are added and removed directly using arrays, subscripts are not re-indexed"
Save space in stack-out stacks, but with low efficiency


The process of executing a function is the structure of a stack: the code that is called after the function is called to complete first, the function that calls is to complete "" "is the code area that is compiled to look up already defined function code"


4. Callback function
What PHP uses is to call those functions that have already been defined in an existing function "called Callback functions"
The use of the Array_map () function is "implemented using a function of your own definition to do a callback."


5. Sorting Processing of arrays
Sort
Key sorting and sorting by value
Sort (): Reorder elements of an array "default ascending"
Ksort (): Sort key Name "Default Ascending" "Keep key value relationship"
Rsort (): Reordering of array elements "descending"
Krsort (): Sort the key names "keep relationships, descending"
Asort (): Sort by value "but keep key-value pair relationship"
"The sorting problem with arrays is that you only need to consider the key-value relationship for the value sort, and the asort () to sort the values."
Natsort () Sort by natural number "The calculated natural number is sorted" "low efficiency"
Usort () Custom Sort "Implementing custom sorting by using callbacks"

Sorting problems, only the need to sort the global order when the database will be sorted, if the problem of local sorting, or PHP to read after the sorting


Sorting algorithm "Focus: Learning with data structures and algorithms"


6. To determine whether a number is a prime, the prime is that there is no number except 1 and itself to divide the data (with a thin algorithm).
<?php
function IsPrime ($n) {
for ($d =2; $d <= $n-1; $d + +) {
Each $d is divisible, which is the most complex algorithm. Essentially, because the number of these loops itself has a recursive relationship, only the preceding primes need to be divisible to determine
if ($n% $d = = 0) {
return false;
}
}
Return true;//If it is not divisible, the prime number
}
?>

Optimized algorithms
(1) First the number cannot be divisible by its 1/2 "because an integer cannot be divided by more than its general integer (the result is 1. more)"
(2) Final optimization of the number of the last integer "In essence, an integer is divided by the number of squares only needed when the square root of the current number (if the square root can not be evenly divisible, it is determined to be prime)"
Comprehensive: In the process of dividing the integer, the divisor itself is corresponding!!!
<?php
function IsPrime ($n) {
$sqrt = sqrt ($n);
for ($d =2; $d <= $sqrt; $d + +) {
if ($n% $d ==0) {
return false;
}
}
Return true;//If it is not divisible, the prime number
}
?>


Problems with loop nesting:
"Continue keyword jumps to the outer loop of the case"
Use of "break inner loop" for break in loop nesting

7. Screening method "An optimization algorithm for prime number"
Suppose that each number is a prime number, and the known range of data is filtered starting from the first (primes) preceding. "To divide the filter, and once it is divisible, mark it as a non-prime number"
Know filter to sqrt value

<?php
function Getprime ($n) {
The data is first populated into the array, and the key is done. Element value is 1 "Make Prime Mark" Array_fill ()
$list = Array_fill (2. $n -1,1);
For ($d =2, $sqrt =sqrt ($n); $d <= $sqrt; $d + +) {
Again to determine whether the known prime
if ($list [$d]==1) {
filtering, starting from twice times the filter element "because the number less than twice is definitely not divisible"
for ($i = $d, $i <= $n; $i + = $d) {
The essence is to find the integer multiples of the $d, starting at twice times $d.
$list [$i]=0;//divide the divisible multiples into 0 processing
}
}
}
Extracts the value of the element in the entire array to 1
foreach ($list as $key = = $value) {
if ($value = = 1) {
echo $key. ' <br/> ';
}
}
}
?>
The above is the implementation of screening!!!

Two algorithms for finding prime numbers:
(1) by dividing "to achieve circular division, only the method of controlling the maximum divisor" to achieve the prime number
(2) Screening method "The maximum divisor has been controlled, and the maximum divisor is marked within, further reducing the number of divisor test"

Each script operation uses the execution time and space settings "This is very important and needs to be combined with your own project business"
"The logic of using space for time is like this."


Also note the difference between isset and empty:
Isset determining whether a variable is defined
Empty equals the opposite of the Boolean value of the value

Array copy, array functions, stack and queue for simulating data structure by array function, meaning of callback, sorting problem of array function, algorithm and filtering method of searching prime number

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.