Basic PHP Algorithms

Source: Internet
Author: User
Bubble Sorting: the number of exchanges between two. The smallest value is on the leftmost, just as the lightest bubble is on the top. The entire column is exchanged for each other. The smallest number is on the leftmost side. Each time, the smallest number in the remaining number is obtained. The arrays popped out form an ordered interval, the rest constitute an unordered interval, and each element in the ordered interval is more than the unordered Interval

Bubble Sorting: the number of exchanges between two. The smallest value is on the leftmost, just as the lightest bubble is on the top. The number of the entire column is exchanged for one time. The smallest number is on the leftmost side. Each time, a minimum number in the remaining number is obtained, and an array popped up into an ordered interval, the rest constitute an unordered interval, and each element in the ordered interval is more than the unordered Interval

Bubble Sorting: exchange values by two. The smallest value is on the leftmost side, just as the lightest bubble is on the top. The number of the entire column is exchanged for one time. The smallest number is on the leftmost side. Each time, a minimum number in the remaining number is obtained, and an array popped up into an ordered interval, the remaining values constitute an unordered interval, and each element value in the ordered interval is smaller than that in the unordered interval.

Quick sorting: the number of baselines, two arrays left and right, recursively called, and merged.

Insert sorting: the sorting interval is divided into two parts. The order on the left and the order on the right are unordered. The first element from the right is inserted into the left interval. If this element is larger than the element on the far right of the Left interval, if the element is smaller than the element on the rightmost side of the left edge range, it is inserted in the original position of the rightmost element. At the same time, the rightmost element is shifted to one place, and the calculator minus one, repeat the preceding steps to repeat the previous steps until the previous element is smaller than the element to be inserted.

Note the processing of the range endpoint value and the subscript of the first element of the array is 0.

// Common PHP sorting algorithms

Function bubblesort ($ array)

{

$ N = count ($ array );

For ($ I = 0; $ I <$ n; $ I ++)

{

For ($ j = $ n-2; $ j >=$ I; $ j --) // [0, I-1] [I, n-1]

{

If ($ array [$ j]> $ array [$ j + 1])

{

$ Temp = $ array [$ j];

$ Array [$ j] = $ array [$ j + 1];

$ Array [$ j + 1] = $ temp;

}

}

}

Return $ array;

}

$ Array = array (, 9, 0, 11 );

Print_r (bubblesort ($ array ));

Echo '';

Function quicksort ($ array)

{

$ N = count ($ array );

If ($ n <= 1)

{

Return $ array;

}

$ Key = $ array ['0'];

$ Array_r = array ();

$ Array_l = array ();

For ($ I = 1; $ I <$ n; $ I ++)

{

If ($ array [$ I]> $ key)

{

$ Array_r [] = $ array [$ I];

}

Else

{

$ Array_l [] = $ array [$ I];

}

}

$ Array_r = quicksort ($ array_r );

$ Array_l = quicksort ($ array_l );

$ Array = array_merge ($ array_l, array ($ key), $ array_r );

Return $ array;

}

Print_r (quicksort ($ array ));

Echo '';

Function insertsort ($ array)

{

$ N = count ($ array );

For ($ I = 1; $ I <$ n; $ I ++) // [0, I-1] [I, n]

{

$ J = $ I-1;

$ Temp = $ array [$ I];

While ($ array [$ j]> $ temp)

{

$ Array [$ j + 1] = $ array [$ j];

$ Array [$ j] = $ temp;

$ J --;

}

}

Return $ array;

}

Print_r (insertsort ($ array ));

?>


========================================================== ======================================


/*
[Insert sort (one-dimensional array )]
[Basic idea]: Insert a data element to be sorted to an appropriate position in the sorted sequence to keep the sequence in order; until all elements of the data to be sorted are inserted.
[Example ]:
[Initial keyword] [49] 38 65 97 76 13 27 49
J = 2 (38) [38 49] 65 97 76 13 27 49
J = 3 (65) [38 49 65] 97 76 13 27 49
J = 4 (97) [38 49 65 97] 76 13 27 49
J = 5 (76) [38 49 65 76 97] 13 27 49
J = 6 (13) [13 38 49 65 76 97] 27 49
J = 7 (27) [13 27 38 49 65 76 97] 49
J = 8 (49) [13 27 38 49 65 76 97]
*/
Function insert_sort ($ arr ){
$ Count = count ($ arr );
For ($ I = 1; $ I <$ count; $ I ++ ){
$ Tmp = $ arr [$ I];
$ J = $ I-1;
While ($ arr [$ j]> $ tmp ){
$ Arr [$ j + 1] = $ arr [$ j];
$ Arr [$ j] = $ tmp;
$ J --;
}
}
Return $ arr;
}


/*
[Select sort (one-dimensional array )]
[Basic idea]: each trip selects the smallest (or largest) element from the data elements to be sorted, and places it at the end of the sorted series, until all data elements to be sorted are arranged.
[Example ]:
[Initial keyword] [49 38 65 97 76 13 27 49]
13 [38 65 97 76 49 27 49] after the first round of sorting
13 27 after the second round of sorting [65 97 76 49 38 49]
13 27 38 after the third round of sorting [97 76 49 65 49]
13 27 38 49 [49 97 65 76]
After the fifth sorting, 13 27 38 49 [97 97 76]
13 27 38 49 49 76 [76 97]
13 27 38 49 76 76 [97]
Last sorting result 13 27 38 49 49 76 76 97
*/
Function select_sort ($ arr ){
$ Count = count ($ arr );
For ($ I = 0; $ I <$ count; $ I ++ ){
$ K = $ I;
For ($ j = $ I + 1; $ j <$ count; $ j ++ ){
If ($ arr [$ k]> $ arr [$ j])
$ K = $ j;
}
If ($ k! = $ I ){
$ Tmp = $ arr [$ I];
$ Arr [$ I] = $ arr [$ k];
$ Arr [$ k] = $ tmp;
}
}
Return $ arr;
}

/*
[Bubble sort (one-dimensional array )]
[Basic idea]: Compare the size of the elements to be sorted in two pairs. If the two data elements are in the opposite order, they are exchanged until there is no data element in reverse order.
[Sorting process]: Imagine the sorted array R [1 .. n] vertical erection. Each data element is considered as a bubble with weight. According to the principle that light bubbles cannot be under heavy bubbles,
Scanning array R from the bottom up, when scanning to a light bubble that violates this principle, it will make it "floating" up, so repeatedly, until the last two bubbles are on the light, so far.
[Example ]:
49 13 13 13 13 13 13 13 13
38 49 27 27 27 27 27
65 38 49 38 38 38 38 38 38
97 65 38 49 49 49
76 97 65 49 49 49 49
13 76 97 65 65 65 65 65
27 27 76 97 76 76 76
49 49 49 76 97 97 97
*/
Function bubble_sort ($ array ){
$ Count = count ($ array );
If ($ count <= 0) return false;

For ($ I = 0; $ I <$ count; $ I ++ ){
For ($ j = $ count-1; $ j> $ I; $ j --){
If ($ array [$ j] <$ array [$ J-1]) {
$ Tmp = $ array [$ j];
$ Array [$ j] = $ array [$ J-1];
$ Array [$ J-1] = $ tmp;
}
}
}
Return $ array;
}

/*
[Quick sorting (one-dimensional array )]
[Basic idea]: In the current disordered zone R [1. H], take a data element as the "benchmark" for comparison (which may be counted as X ),
Use this benchmark to divide the current unordered zoning into two smaller unordered zones: R [1 .. i-1] and R [I 1 .. h], and the data elements in the unordered subarea on the left are smaller than or equal to the baseline element,
The data elements in the unordered subarea on the right are greater than or equal to the benchmark element, while the benchmark X is located at the final sorting position, that is, R [1 .. i-1] ≤ X. key ≤ R [I 1 .. h] (1 ≤ I ≤ H ),
When R [1 .. i-1] and R [I 1 .. h] when they are not empty, they are divided separately until all data elements in the unordered subarea are sorted.
[Example ]:
Initial keyword [49 38 65 97 76 13 27 49]
After the first exchange [27 38 65 97 76 13 49 49]
After the second exchange [27 38 49 97 76 13 65 49]
J left scan, position unchanged, after the third exchange [27 38 13 97 76 49 65 49]
I scan right, the position remains unchanged, after the fourth switch [27 38 13 49 76 97 65 49]
J left scan [27 38 13 49 76 97 65 49]
(One division process)

Initial keyword [49 38 65 97 76 13 27 49]
[27 38 13] 49 [76 97 65 49]
[13] 27 [38] 49 [49 65] 76 [97]
13 27 38 49 [65] 76 97
Last sorting result 13 27 38 49 65 76 97
Status after sorting
*/
Function quick_sort ($ array ){
If (count ($ array) <= 1) return $ array;
$ Key = $ array [0];
$ Left_arr = array ();
$ Right_arr = array ();
For ($ I = 1; $ I If ($ array [$ I] <= $ key)
$ Left_arr [] = $ array [$ I];
Else
$ Right_arr [] = $ array [$ I];
}
$ Left_arr = quick_sort ($ left_arr );
$ Right_arr = quick_sort ($ right_arr );

Return array_merge ($ left_arr, array ($ key), $ right_arr );
}

/* Print all the content of the array */
Function display_arr ($ array ){
$ Len = count ($ array );
For ($ I = 0; $ I <$ len; $ I ++ ){
Echo $ array [$ I]. '';
}
Echo'
';
}

/*
Comparison and Selection of several sort Algorithms
1. factors to consider when selecting the sorting method:
(1) number of elements to be sorted n;
(2) The size of the information of the element itself;
(3) structure and distribution of keywords;
(4) conditions of language tools, sizes of auxiliary space, and so on.
2. Summary:
(1) If n is small (n <= 50), you can directly insert or select sorting. Because the movement of records required for direct insertion and sorting is more direct, it is better to directly select and sort records when the amount of information in the records is large.
(2) If the initial state of the file has been basically ordered by the keyword, it is appropriate to select direct insertion or Bubble sorting.
(3) If n is large, the time complexity is O (nlog2n. Quick sorting is currently the best method in comparison-based internal sorting.
(4) In the comparative sorting method, after comparing the size of two keywords each time, only two possible transfer occurs. Therefore, a binary tree can be used to describe the comparison and determination process, it can be proved that when the n key words of the file are randomly distributed, any sort algorithm by means of "comparison" requires at least O (nlog2n) time.
(5) When the record itself contains a large amount of information, you can use a linked list as the storage structure to avoid time-consuming moving records.
*/

/* Sort test */

$ A = array ('12', '4', '16', '8', '13', '20', '5', '32 ');

Echo 'the result of insert sort :';
$ Insert_a = insert_sort ($ );
Display_arr ($ insert_a );

Echo 'the result of select sort :';
$ Select_a = select_sort ($ );
Display_arr ($ select_a );

Echo 'the result of bubble sort :';
$ Bubble_a = bubble_sort ($ );
Display_arr ($ bubble_a );

Echo 'the result of bubble sort :';
$ Quick_a = quick_sort ($ );
Display_arr ($ quick_a );

?>

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.