Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to implement Common sorting algorithms with PHP

Shulou Source: shulou.com Published: 2022-06-01 14:30:48 09月21日 Update

This article mainly explains "how to achieve common sorting algorithms in PHP". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "PHP how to achieve common sorting algorithms" it!

Post a picture to analyze the time complexity and stability of common algorithms.

A lot of phper can't do algorithms, and ordinary web developers can't use them, but why should I learn. Just in order to meet programmers who can't do algorithms, they can hang and beat them! Keep a low profile

1. Bubbling sorting method

Bubble sorting is to move small elements forward (or large elements back). Note that the two adjacent elements are compared, and whether or not the exchange is required also occurs between the two elements.

So, if the two elements are equal, I don't think you'll be bored to exchange them again.

If two equal elements are not adjacent, then even if the two elements are adjacent by the previous pairwise exchange, the positions of the two elements will not be exchanged in the end, so the order of the same elements has not changed after sorting.

Therefore, bubble sorting is a stable sorting algorithm.

two。 Selective sorting method

To select sort is to select the current smallest element of the element to be sorted for each location. For example, choose the smallest one for the first position and the second one for the second position in the remaining elements.

And so on, until the nth element, the nth element does not have to be selected, because it is only the largest element left.

So, during a selection, if the currently locked element is larger than the latter element, and the latter smaller element appears behind an element equal to the currently locked element, the order of positions after the exchange obviously changes.

He he! Compare a mouthful, for example: sequence 58 529, we know that the first choice of element 5 will be exchanged with 2, then the relative sequence of the two fives in the original sequence will be destroyed.

Therefore, selective sorting is not a stable sorting algorithm.

At this point, I believe that everyone on the "PHP how to achieve common sorting algorithms" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Elements sorting selection algorithm two location common order learning minimum content that is sequence complex practical mouthful deeper maximum same boring Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MySQL Xiaomi Microsoft MariaDB