Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Common sorting algorithms with PHP

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report