In this article, I will introduce you to the test of the sorting algorithm written in PHP. The following are 14 sort algorithms: fast sort count sort heap sort merge sort Hill sort select sort insert sort place sort combined bubble sort cocktail sort bubble sort parity
In this article, I will introduce you to the test of the sorting algorithm written in PHP.
The following are 14 sorting algorithms:
- Quick sorting
- Count sorting
- Sort by comb
- Heap sorting
- Merge sorting
- Hill Sorting
- Select sort
- Insert sort
- Refined geographic sorting
- Combined bubble sorting
- Cocktail sorting
- Bubble sorting
- Parity sorting
- Sort by flags in bubble mode
The algorithm does not sort by letters, but sorts by elements at a descending speed.
The following is the size of the array used:
- 1
- 100
- 200
- 400
- 600
- 800
- 1000
- 5000
- 10000
- 15000
- 20000
- 25000
- 30000
Each measurement uses an array of different sizes and is passed into the sorting function.
- In the first case, the array is randomly filled with values (1, N), where N is the size of the index group.
- In the second case, the array is randomly filled with values (1, PHP_INT_MAX). PHP_INT_MAX indicates the maximum value of the INT type in the current system, 2 ^ 63 in my system or about 9.2233720425548e + 18.
Each test is performed three times and its arithmetic average value is obtained.
Array of the 1000 elements
Sort all algorithms of the current array size.
Array of the 30000 elements
In this case, the five fastest algorithms are tested: counting sorting, fast sorting, combing sorting, heap sorting, and merge sorting.
Array of the 200000 elements
In this case, the five fastest algorithms are tested: counting sorting, fast sorting, combing sorting, heap sorting, and merge sorting.
Array of the 2000000 elements
In the last round of 2000000 elements, only two algorithms are available for testing: count sorting and quick sorting.
Summary
Quick sorting is a good algorithm that is named by name. Counting sorting works well in a small value range; in other cases, it cannot be paid because of low memory. Cocktail sorting is a bad choice for random values. Bubble sorting and its deformation are not suitable for practical applications.
Source code + result: https://drive.google.com/file/d/0B63HSL7JD630VWdSSFgwdHR5RkU/edit? Usp = sharing
Using the built-in sorting function is an interesting exercise. Using interpreted PHP to write sorting functions is always faster than the C variant used by sort.