Graphical Sort Algorithm (2) Hill Sort

IT Open Net 13/07/2021 142

Hill Sort is a sorting algorithm proposed in Donald Shell in 1959. Hill sorting is also a more efficient version of the simple insertion sorting after improvement, also known as a reduced incremental order, and the algorithm is one of the first batch of algorithms of O (N2). This article will detail the basic idea of Hill's sorting and its code implementation in the way.

基本思想

Hill sort is a certain incremental grouping of records, sorting the direct insertion algorithm for each group; with the gradual decrease in increments, more and more keywords included, when the increment is reduced to 1, The entire file is directly divided into a group, and the algorithm is terminated.

Simple insertion sorting is very changing, regardless of the array distribution, one step, one step, one step, the element is compared, mobile, inserted, such as [5, 4, 3, 2, 1, 0] this reverse sequence, the number of ends of the array end It is necessary to return to the first place. It is very hard, comparing and moving elements need N-1. And Hill sorted in an array using a jumping grouping, dividing an array element into a number of groups through an increment, then packet insertion sorting, then gradually narrowing the increment, continue inserting insertion ordering operation until increment To 1. Hill Sort Through this strategy makes the entire array reaches the basic orderly, small basic in the initial stage, and the substantially basically the basics. The increment is then reduced, and when the increment is 1, in fact, it is only necessary to fine tuning, and too much data movement will not be involved.

Let's take a look at the basic steps sorted by Hill, where we choose increment GAP = Length / 2, narrow the increment continues to use a GAP = GAP / 2 method, this incremental selection we can use a sequence to represent, { N / 2, (N / 2) / 2 ... 1}, called an incremental sequence. The choice of incremental sequence of Hill sorted is a mathematical problem. The incremental sequence we choose is more common and the increase in Hill recommends, called Hill Increment, but in fact this incremental sequence is not the most Excellent. Here we do an example using Hill increment.

Graphical Sort Algorithm (2) Hill Sort  代码实现

When the understanding of Hill, we tend to process each group, in groups, but in the code implementation, we can handle a set of re-step back to handle the next group (so you have to add it. For loop deduction grouping), such as [5, 4, 3, 2, 1, 0], first incremental gap = length / 2 = 3, then 3 groups [5, 2] [4, 1] [3, 0] When implemented, it is not used by group processing, we can start from the GBEP elements and processed one by one. At the same time, when the data is inserted, the element exchange method can be used to find the final position, or the array element movement can be used. Hill sorted code is relatively simple, as follows:

Graphical Sort Algorithm (2) Hill Sort  Graphical Sort Algorithm (2) Hill Sort  Graphical Sort Algorithm (2) Hill Sort  总结

This paper introduces the basic idea of Hill's sorting and its code implementation, and the choice of incremental sequence in Hill is very important to directly affect the performance of Hill. We have selected incremental sequence {n / 2, (n / 2) / 2 ... 1} (Hill increment), the worst time complexity is still O (N2), some optimized increments Sequences such as Hibbard has made the worst time complexity O (N3 / 2). The introduction of Hill sorting is here, and the introduction of other sorting algorithms will also be updated, thank you for your support.

Latest: ZW32-40.5 35kV column vacuum circuit breaker profile size diagram - installation diagram

Next: How to make MAC "visit" better?