PHP sort algorithm for Hill sort (Shell sort)

Source: Internet
Author: User
This article mainly introduced the PHP sorting algorithm of the hill sort (Shell sort), combined with the case of the form of a detailed analysis of the principle of hill sorting, implementation methods and related considerations, the need for friends can refer to the next

The example in this article describes the PHP sort algorithm in the hill sort (Shell sort). Share to everyone for your reference, as follows:

Basic idea:

Hill sort refers to a certain increment grouping of records by subscript, the use of direct insert sorting for each group, the increasing number of keywords per group as the increment decreases, and when the increment is reduced to 1 o'clock, the entire sequence is divided into a group, and the algorithm terminates.

Operation Steps:

First, take an integer less than n (number of sequential records) D1 as the first increment, grouping all the records in the file. All records with a multiple of D1 are placed in the same group. First, the direct insertion sort is done in each group, then the second increment D2 < D1 repeats the above groupings and sorts until the increment dt=1 (dt < D (t-1) ...< D2 < D1), that is, all records are placed in the same group for direct insert sorting.

This method is essentially a grouping insertion method

Comparing the number of distant distances (called increments) so that the number can move across multiple elements, it is possible to eliminate multiple element exchanges at once than [2]. D.l.shell realized this idea in 1959 in a sort algorithm named after him. The algorithm first sorts the group of numbers by an increment d into groups, each group of records of the subscript difference D. Sort all the elements in each group, then use a smaller increment to do it, and then sort them in each group. When the increment is reduced to 1 o'clock, the entire number to be sorted is divided into a group, and the sort is completed.

The average initial fetch sequence is half-increment, and then halved each time until the increment is 1.

As for the increment method, it is said that there is no one of the best incremental sequences yet, but there is a strong requirement that the last increment value must be equal to 1.

Sort procedure for Shell ordering for a given instance

Suppose the file to be sorted has 10 records whose keywords are:

49,38,65,97,76,13,27,49,55,04.

The increment sequence takes the following values:

5,3,1

Algorithm implementation:

<?php//Hill Sort (improvement of the direct insertion sort) function Shellsort (array & $arr) {  $count = count ($arr);  $inc = $count;  Increment do  {/    /calculate increment    //$inc = Floor ($inc/3) + 1;    $inc = Ceil ($inc/2);    for ($i = $inc; $i < $count; $i + +) {      $temp = $arr [$i];  Set Sentinel      //need to insert $temp into ordered increment sub-table for      ($j = $i-$inc; $j >= 0 && $arr [$j + $inc] < $arr [$J]; $j-= $inc) {        $arr [$j + $inc] = $arr [$j];//record-shift      }      Insert      $arr [$j + $inc] = $temp;    }    Increment to 1 o'clock stop loop  } while ($inc > 1);} $arr = Array (9,1,5,8,3,7,4,6,2); $arr = Array (49,38,65,97,76,13,27,49,55,04); Shellsort ($arr); Var_dump ($arr);

Operation Result:

Array (Ten) {[0]=> int (4) [1]=> int (all) [2]=> int ([4]=>) [3]=> int (+) [5]=> int ($) [6]=> int ($) int ([7]=>) int ([8]=> int) [9]=> int (97)}

Analysis of Complexity:

Through the above code analysis, I believe that we have some understand that the key to the hill is not a random grouping after the respective sorting, but will be separated by a "increment" of the record into a sub-sequence, to achieve a jump-type movement, so that the efficiency of the sequencing.

In the worst case, the time complexity is O (n^2).

Hill sort is an unstable sort.

This article refers to from the "Big Talk Data Structure", in this only record, convenient to consult later, great God do not spray!

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.