In this article, I will introduce to you the testing of sorting algorithms written in PHP.
The following are 14 sorting algorithms:
The algorithms are not sorted alphabetically, but rather sorted by their overall decreasing speed when sorting 8 thousand elements.
The following are the sizes of the arrays used:
Use an array of different sizes for each measurement and then pass it into the sorting function.
Each test is performed 3 times, and then the arithmetic mean is taken.
All algorithmic sorting cases at the current array size.
At this point, 5 fastest algorithms are tested: counting sort, quick sort, comb sort, heap sort and merge sort.
At this point, 5 fastest algorithms are tested: counting sort, quick sort, comb sort, heap sort and merge sort.
In the final round of testing with 2,000,000 elements, only 2 algorithms were tested: counting sort and quick sort.
Quick sort is a well-deserved good algorithm. Counting sort performs well in small ranges of values; otherwise it cannot cope with low memory. Cocktail sorting is a bad choice for random values. Bubble sort and its variants are not suitable for practical applications.
Source code + results of all algorithms: https://drive.google.com/file/d/0B63HSL7JD630VWdSSFgwdHR5RkU/edit?usp=sharing
Using the built-in sorting functions is a fun exercise. Writing sorting functions in interpreted PHP will never be faster than the C variant of sort().
Translation link: http://blog.jobbole.com/68774/