Home  >  Article  >  Backend Development  >  Test evaluation: 14 sorting algorithms and PHP arrays_PHP tutorial

Test evaluation: 14 sorting algorithms and PHP arrays_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:54:03829browse

In this article, I will introduce to you the testing of sorting algorithms written in PHP.
The following are 14 sorting algorithms:

  • Quick sort
  • Counting sort
  • Comb sorting
  • Heap sort
  • Merge sort
  • Hill sort
  • Select sort
  • Insertion sort
  • Goblin sorting
  • Union Bubble Sort
  • Cocktail sorting
  • Bubble sort
  • Odd-even sort
  • Bubble sort using flags

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:

  • 1
  • 100
  • 200
  • 400
  • 600
  • 800
  • 1000
  • 5000
  • 10000
  • 15000
  • 20000
  • 25000
  • 30000

Use an array of different sizes for each measurement and then pass it into the sorting function.

  • In the first case, the array is randomly filled with values ​​between 1, N), where N refers to the size of the array.
  • In the second case, the array is randomly filled with values ​​between 1, PHP_INT_MAX), where PHP_INT_MAX refers to the maximum value of INT type in the current system, which in my system is 2^63 or about 9.2233720368548E+ 18.

Each test is performed 3 times, and then the arithmetic mean is taken.

Array of 1000 elements

All algorithmic sorting cases at the current array size.

Array of 30000 elements

At this point, 5 fastest algorithms are tested: counting sort, quick sort, comb sort, heap sort and merge sort.

Array of 200000 elements

At this point, 5 fastest algorithms are tested: counting sort, quick sort, comb sort, heap sort and merge sort.

Array of 2,000,000 elements

In the final round of testing with 2,000,000 elements, only 2 algorithms were tested: counting sort and quick sort.

Summary

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/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/778654.htmlTechArticleIn this article, I will introduce to you the testing of sorting algorithms written in PHP. The following are 14 sorting algorithms: Quick sort Counting sort Comb sort Heap sort Merge sort Hill sort...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn