<?php function qsort(&$arr) { _quick_sort($arr, 0, count($arr) - 1); } function _quick_sort(&$arr, $low, $high) { $low_data = $arr[$low]; $prev_low = $low; $prev_high = $high; while ($low < $high) { while ($arr[$high] >= $low_data && $low < $high) { $high--; } if ($low < $high) { $arr[$low] = $arr[$high]; $low++; } while ($arr[$low] <= $low_data && $low < $high) { $low++; } if ($low < $high) { $arr[$high] = $arr[$low]; $high--; } }
Quick sort using recursive algorithm.
@param array $arr The array to be sorted
@param int $low The lowest sorting subsection
@param int $high The highest sorting field
All resources on this site are contributed by netizens or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this site are for learning reference only. Please do not use them for commercial purposes. Otherwise, you will be responsible for all consequences! If there is any infringement, please contact us to delete it. Contact information: admin@php.cn
Related Article
29 Jul 2016
Quick sort, php: Quick sort PHP implementation: /**** Quick sort: unstable, the optimal time complexity is O(nlogn) and the worst time is O(n^2)* Quick sort is a kind of bubble sort Substantial improvement. Its basic idea is that after one scan,* the length of the sorted sequence can be greatly reduced. In bubble sort, a scan can only ensure that the number with the largest value moves to the correct position, * and the length of the sequence to be sorted may only be reduced by 1. Quick sort can ensure that all the numbers on the left of a certain number (let's use it as the base point) are smaller than it, and * all the numbers on the right are larger than it through one scan. Then use the same method
19 Feb 2024
Title: Efficient method and code example to implement quick sort algorithm in Java Introduction: Quick sort is an efficient sorting algorithm, which is based on the idea of divide and conquer and has better performance under average circumstances. This article will introduce the implementation process of the quick sort algorithm in detail through Java code examples, along with performance optimization tips to improve its efficiency. 1. Algorithm principle: The core idea of quick sort is to select a benchmark element and divide the sequence to be sorted into two subsequences through one sorting. The elements of one subsequence are smaller than the benchmark element, and the elements of the other subsequence are smaller than the benchmark element.
08 Apr 2018
This article introduces the code for quick sorting in the PHP algorithm. Now I share it with you. Friends in need can also refer to it. Let’s take a look.
Hot Tools
PHP library for dependency injection containers
PHP library for dependency injection containers
A collection of 50 excellent classic PHP algorithms
Classic PHP algorithm, learn excellent ideas and expand your thinking
Small PHP library for optimizing images
Small PHP library for optimizing images