Insert sort, quick sort, and PHP object-oriented knowledge

Source: Internet
Author: User
Insert sort, quick sort, and PHP object-oriented knowledge

External sorting method:

Because the data is too large to handle so much data, it is an external sort to split the big data into small data. Use disk to store files.

Sorting algorithm: Insert Sort method.

To arrange the array from small to large as an example, the array to be sorted is treated as two parts: the first part is an ordered array. The default length is 1, which is the element labeled 0. All subsequent elements form an unordered array, each time the first element of the unordered array $arr[i] is added to the ordered array, if $ARR[I] is greater than $arr[i-1], the element is added directly to the end (that is, no changes are made), if $arr[i] is less than $arr[i-1), the $arr [i] is inserted into an appropriate position in an ordered array, and the sort process ends when the sequence is expressed to the original array size.

Sorting efficiency:

Bubble sort<><>< span=""><><>

The faster the efficiency, the more system resources are occupied. Fast sorting is fast, but it takes a lot of memory because it involves a lot of recursive calls.

Insert Sort code Example:

$arr = Array ( -2,3,5,4,9,7,15,11,1,6), echo Count ($arr)-1;

for ($i =1; $i < span="">

if ($arr [$i]> $arr [$i-1]) {continue;}

for ($j =0; $j < $i; $j + +) {

if ($arr [$i]< $arr [$j]) {

$temp = $arr [$i];

for ($k = $i; $k >= $j; $k-) {

$arr [$k]= $arr [$k-1];


$arr [$j]= $temp;


}echo "
". Print_r ($arr)."


Quick Sort Method:

function Quicksort ($SEQ) {

if (count ($seq) > 1) {

$k = $seq [0];

$x = Array ();

$y = Array ();

for ($i =1; $i < span="">

if ($seq [$i] <= $k) {

$x [] = $seq [$i];

} else {

$y [] = $seq [$i];



$x = Quicksort ($x); Sort by array X

$y = Quicksort ($y);

Return Array_merge ($x, Array ($k), $y);

}else {

return $seq;



$arr = Array (12,2,16,30,8,28,4,10,20,6,18);

Print_r (Quicksort ($arr));

There are multiple forms on the same page, which is not known when submitting to PHP processing, by setting a name value for each form to a different hidden control. Determine which form was submitted by looking at the value of the hidden control

The idea of object-oriented programming in PHP

PHP object in memory Area: PHP Object name and object location is not the same, this is the same as Java, so in the object-oriented part of PHP You can fully apply The idea of Java to do. objects are stored in the heap and the object names are stored in the stack just like any other normal variable. Note that the object name itself is a reference to an object, that is, its value is actually the address of the object it points to, so you can change the object it points to to point to an entirely different object, so that an object can have multiple object names, but be aware that Modifying the member property of one of the object names causes the member properties pointed to by other object names to also change.

The base type variables and object names are stored in the buyers area, and the objects are stored in the heap area. Then the object name is a reference, that is, the value is the address, and the base type variable is only added & address characters represents the address. Note the use of this keyword, especially the member variables of the zone class and the local variables in the member functions.

  • 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: 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.