PHP implementation of array sorting methods: fast sorting, inserting sorting, merging sorting algorithm

Source: Internet
Author: User
Tags array sort

There are many ways to sort arrays in PHP, and each array sort has its own different principles, and here's a look at examples of fast sorting algorithms, merge sorting algorithms, and insertion sorting algorithms.

Traversal of an irregular array

Find the average of the numbers in the following array:

$arr 1 = Array (1, 2, Array (), 4,array (n, N, +), array (541, 542, 543, 544), 6, array (0, A,); Count $sum = getarraysum ($arr 1); echo "\

Fast sorting algorithm

Principle Description:

For such an array: [5, 1, 2, 6,7];

Take out the first item (and act as an intermediate array) and compare the remaining items to two arrays:

The left array item is smaller than the middle item, and the right array is no smaller than the middle.

If the left and right arrays are already well-ordered arrays, merging the 3 is the final result.

If the left and right arrays are not yet a well-ordered array, continue to use this function recursively to get an ordered array.

Schematic diagram:

Original Rational Data:

$arr 1 = [5, 2, 1, 6,7]; Data for a strong explanation of the principles 1

Small: [2, 1], large: [6, 7], Middle: [5]

Merge the three: [1, 2, 5, 6, 7];

$arr 1 = [2, 1]; Data for a strong explanation of the principles 2

Middle: [2], Left: [1], []

Specific cases:

$arr 1 = [5, 2, 4, 6, 1, 3]; $arr 1 = [5, 2, 4, 6, 1, 3];//$arr 1 = [5, 3, 2, 8, 7];echo "\

Insert Sort algorithm

Principle Description:

For such an array: [2, 3, 4, 1];

To insert a number of n into an array that is already sorted,

Just compare n with the item of this array from the back to the next, as long as you find something larger than N,

Move the item back one bit, and then continue to take it out and contrast it, moving one bit farther than N, and so on.

In the end, when there is no greater than N, put N into the position of the space-time just behind the move.

For an array, the 1th item can be used as an "already ordered" array,

Then the 2nd item can be "inserted sort" according to the above principle, so the first two can be arranged,

And becomes a "well-ordered array" with two elements. Follow up and so on.

Schematic diagram:

Principle Data:

$arr 1 = [2, 3, 4, 1]; Data for a strong explanation of the principles 1

$arr 1 = [2, 3, 1]; Data for a strong explanation of the principles 2

$arr 1 = [2, 1]; Data for a strong explanation of the principles 3

$arr 1 = [1, 2]; Data for a strong explanation of the principles 3

Specific cases:

$arr 1 = [5, 2, 4, 6, 1, 3]; $arr 1 = [2, 3, 4, 1]; $arr 1 = [2, 4, 5, 6, 1, 3];echo "\

Merge sort algorithm

Principle Description:

For such an array: $arr 1 = [1, 3, 5, 2, 4, 6]; split it in Split: $a = [1, 3, 5],
$b = [2, 4, 6];

If you have two arrays that are already ordered, you can get a "fused array" of the two arrays by doing the following:

Take out the first item of array a A1, then take out the first item of array B b1, compare the size of A1 and B1,

and put the small (assuming the A1) into a new array, and to delete the corresponding array A of the first item,

Then we take the first item of the corresponding array (not the data just now) and then continue to compare the two sizes

Each time you put a small into the new array, and continue to the next time, "Delete, take the number, compare" ....

The end result is that a new array can be sorted in a new sequence.

For an array that has not yet been ordered, as long as it recursively continues to "split in two", it will eventually get the shortest array-only one or 0 units, which is naturally ordered.

Schematic diagram:

Principle Data:

$arr 1 = [1, 3, 5, 4, 6, 7, 8]; Data for a strong explanation of the principles 1

From the middle one is 2: []; [6, 7, 8]

[1, 3, 4, 5,]

$arr 1 = [1, 3, 2, 4]; Data for a strong explanation of the principles 2

Demo Case:


$arr 1 = [5, 2, 4, 6, 1, 3];echo "\
Related Article

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.