PHP compares the running time of four basic sorting algorithms (recommended) and four algorithms

Source: Internet
Author: User

PHP compares the running time of four basic sorting algorithms (recommended) and four algorithms

Many people say that algorithms are the core of a program. The quality of algorithms determines the quality of the program. As a beginner phper, although seldom touched on algorithms. However, the basic sorting algorithm should still be mastered. It is a necessary tool for program development. The following describes how to compare the running time of four basic sorting algorithms in PHP.

If you don't talk much about it, You can directly post code to everyone.

The Code is as follows:

/*** Php four basic sorting algorithm running time comparison * @ authors Jesse (jesse152@163.com) * @ date 07:12:14 * // Bubble Sorting function bubbleSort ($ array) {$ temp = 0; for ($ I = 0; $ I <count ($ array)-1; $ I ++) {for ($ j = 0; $ j <count ($ array)-1-$ I; $ j ++) {if ($ array [$ j]> $ array [$ j + 1]) {// arranged from small to large $ temp = $ array [$ j]; $ array [$ j] = $ array [$ j + 1]; $ array [$ j + 1] = $ temp ;}}// select the sorting function selectSort ($ array) {$ temp = 0; for ($ I = 0; $ I <count ($ array )- 1; $ I ++) {$ minVal = $ array [$ I]; // assume that $ I is the minimum value $ minValIndex = $ I; for ($ j = $ I + 1; $ j <count ($ array); $ j ++) {if ($ minVal> $ array [$ j]) {// arrange from small to large $ minVal = $ array [$ j]; // find the minimum value $ minValIndex = $ j ;}$ temp = $ array [$ I]; $ array [$ I] = $ array [$ minValIndex]; $ array [$ minValIndex] = $ temp; }}// insert sorting function insertSort ($ array) {// sort from small to large. // The default value is $ array [0]. It is an ordered table for ($ I = 1; $ I <count ($ array ); $ I ++) {$ insertVal = $ array [$ I]; // $ in SertVal is the number of inserts to be prepared $ insertIndex = $ I-1; // subscript of the number to be compared in an ordered table while ($ insertIndex> = 0 & $ insertVal <$ array [$ insertIndex]) {$ array [$ insertIndex + 1] = $ array [$ insertIndex]; // move the array back $ insertIndex --; // move the subscript forward, prepare to compare with the previous one.} if ($ insertIndex + 1! ==$ I) {$ array [$ insertIndex + 1] = $ insertVal ;}}// quick sort function quickSort ($ array) {if (! Isset ($ array [1]) return $ array; $ mid = $ array [0]; // obtain a keyword for segmentation, the first element $ leftArray = array (); $ rightArray = array (); foreach ($ array as $ v) {if ($ v> $ mid) $ rightArray [] = $ v; // put the number larger than $ mid into an array if ($ v <$ mid) $ leftArray [] = $ v; // put a number smaller than $ mid into another array} $ leftArray = quickSort ($ leftArray ); // split the small array again $ leftArray [] = $ mid; // Add the split element to the back of the small array, you can't forget it. $ rightArray = quickSort ($ rightArray); // You can split the larger array again. return array_merge ($ leftArray, $ rightArray ); // combine two results} $ a = array_rand (range (1600), 1600); // generate a random array shuffle ($ a) with elements ); // disrupt the array order $ t1 = microtime (true); bubbleSort ($ a); // bubble sort $ t2 = microtime (true); echo "bubble sort :". ($ t2-$ t1) x 1000 ). 'ms '. "\ n"; $ t3 = microtime (true); selectSort ($ a); // select sort $ t4 = microtime (true); echo "when sorting is selected :". ($ t4-$ t3) * 1000 ). 'ms '. "\ n"; $ t5 = microtime (true); insertSort ($ a); // insert sort $ t6 = microtime (true); echo "insert sort :". ($ t6-$ t5) x 1000 ). 'ms '. "\ n"; $ t7 = microtime (true); quickSort ($ a); // quick sorting $ t8 = microtime (true); echo "quick sorting :". ($ t8-$ t7) * 1000 ). 'ms ';

The above section describes how to compare the running time of four basic sorting algorithms implemented by PHP. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.