Php implements Method Analysis of the hill sorting algorithm, and php Hill Sorting Algorithm

Source: Internet
Author: User

Php implements Method Analysis of the hill sorting algorithm, and php Hill Sorting Algorithm

This example describes how to implement the hill Sorting Algorithm in php. We will share this with you for your reference. The details are as follows:

Although various programming languages have their own powerful sorting database functions, these underlying implementations also use these basic or advanced sorting algorithms.

It is interesting to understand these complex sorting algorithms ~

Shell sort: the sort by hill is based on the insert sort. The difference is that the insert sort is a comparison of adjacent values (similar to h = 1 in Hill ), hill sorting is a comparison and replacement of distance h.

The original array is divided into groups. Each group is composed of h elements, which may have redundant elements. Of course, h is also decreasing in every loop (h = h/n ). The first cycle starts with the subscript h. One idea of Hill's sorting is to sort by group.

To understand these algorithms, it is best to have an illustration. Let's start with the code.

<? Php/*** sort by hill */function shell_sort (array $ arr) {// sort $ arr in ascending order $ len = count ($ arr); $ f = 3; // defines the factor $ h = 1; // the minimum value is 1 while ($ h <$ len/$ f) {$ h = $ f * $ h + 1; // 1, 4, 13, 40,121,364,109 3 ,...} while ($ h> = 1) {// convert the array to h ordered for ($ I = $ h; $ I <$ len; $ I ++) {// insert a [I] into a [I-h], a [I-2 * h], a [I-3 * h]... (Key of the algorithm for ($ j = $ I; $ j >=$ h; $ j-= $ h) {if ($ arr [$ j] <$ arr [$ j-$ h]) {$ temp = $ arr [$ j]; $ arr [$ j] = $ arr [$ j-$ h]; $ arr [$ j-$ h] = $ temp;} // print_r ($ arr ); echo '<br/>'; // open this line of comment and you can see the situation where each step is replaced} $ h = intval ($ h/$ f );} return $ arr;} $ arr = array (14, 9, 1, 4, 6,-3, 2, 99, 13, 20, 17, 15, 3 ); $ shell = shell_sort ($ arr); echo '<pre>'; print_r ($ shell ); /*** Array ([0] =>-3 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 6 [6] => 9 [7] => 13 [8] => 14 [9] => 15 [10] => 17 [11] => 20 [12] => 99 )) **/

PS: here we recommend a demo tool for sorting for your reference:

Online animation demo insert/select/bubble/merge/hill/Quick Sort Algorithm process tool:
Http://tools.jb51.net/aideddesign/paixu_ys

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.