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 realize Quick sort and Bubble sort by PHP

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

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

1. Quick sorting

1. Brief introduction

Quick sorting is a sort algorithm developed by Tony Hall. On average, sorting n items requires n log n comparisons. In the worst case, you need to compare (N2) times, but this is not common. In fact, quick sorting is usually significantly faster than other n log n algorithms because its internal loop (inner loop) can be implemented efficiently on most architectures.

Quick sort uses the divide-and-conquer (Divide and conquer) strategy to divide a serial (list) into two subserial (sub-lists).

two。 Steps

Pick an element from a series, called a "pivot"

Reorder the series so that all elements smaller than the benchmark are placed in front of the benchmark, and all elements larger than the benchmark are placed behind the benchmark (the same number can be on either side). After the partition exits, the datum is in the middle of the series. This is called a partition operation.

Recursive sorts the subseries of elements less than the reference value and the subseries of elements greater than the reference value.

3. Code implementation

The copy code is as follows:

Function quickSort (array $array)

{

$len = count ($array)

If ($len

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

Development

Wechat

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

12
Report