Home Java javaTutorial Detailed interpretation of Hill sorting algorithm and related Java code implementation

Detailed interpretation of Hill sorting algorithm and related Java code implementation

Jan 19, 2017 am 09:20 AM

Hill sort (Shell's sort) is a very "magical" sorting algorithm. It is called "magical" because no one can clearly explain what its performance is. Hill sorting due to DL. Shell was named after it was proposed in 1959. Since C. A. R. Hoare proposed quick sort in 1962, quick sort is generally used because it is simpler. However, many mathematicians are still working tirelessly to find the optimal complexity of Hill sorting. As ordinary programmers, we can learn from Hill's ideas.
By the way, before the emergence of Hill sorting, there was a common view in the computer industry that "sorting algorithms cannot break through O(n2)". The emergence of Hill sorting broke this curse, and soon, algorithms such as quick sort came out one after another. In this sense, Hill sorting leads us into a new era.

Algorithm overview/ideas
The proposal of Hill sorting is mainly based on the following two points:
1. The insertion sort algorithm can approximately reach O(n) complexity when the array is basically ordered. degree, extremely efficient.
2. However, insertion sort can only move data one bit at a time, and the performance will deteriorate rapidly when the array is large and basically disordered.

Based on this, we can use a grouped insertion sort method. The specific method is: (take a 16-element array as an example)
1. Select an increment delta, which is greater than 1. Select the subarray from the array according to this increment for a direct insertion sort. For example, if the selected increment is 5, the elements with indexes 0, 5, 10, and 15 will be sorted.
2. Keep the incremental delta and move the first element in sequence for direct insertion sorting until one round is completed. For the above example, the arrays [1, 6, 11], [2, 7, 12], [3, 8, 13], [4, 9, 14] are sorted in sequence.
3. Reduce the increment and repeat the above process until the increment is reduced to 1. Obviously, the last time is direct insertion sorting.
4. Sorting completed.
As can be seen from the above, the increment is constantly decreasing, so Hill sorting is also called "shrinking increment sorting".

Code implementation

package sort; 
  
public class ShellSortTest { 
  public static int count = 0; 
  
  public static void main(String[] args) { 
  
    int[] data = new int[] { 5, 3, 6, 2, 1, 9, 4, 8, 7 }; 
    print(data); 
    shellSort(data); 
    print(data); 
  
  } 
  
  public static void shellSort(int[] data) { 
    // 计算出最大的h值 
    int h = 1; 
    while (h <= data.length / 3) { 
      h = h * 3 + 1; 
    } 
    while (h > 0) { 
      for (int i = h; i < data.length; i += h) { 
        if (data[i] < data[i - h]) { 
          int tmp = data[i]; 
          int j = i - h; 
          while (j >= 0 && data[j] > tmp) { 
            data[j + h] = data[j]; 
            j -= h; 
          } 
          data[j + h] = tmp; 
          print(data); 
        } 
      } 
      // 计算出下一个h值 
      h = (h - 1) / 3; 
    } 
  } 
  
  public static void print(int[] data) { 
    for (int i = 0; i < data.length; i++) { 
      System.out.print(data[i] + "\t"); 
    } 
    System.out.println(); 
  } 
  
}
Copy after login

Running results:

5  3  6  2  1  9  4  8  7   
1  3  6  2  5  9  4  8  7   
1  2  3  6  5  9  4  8  7   
1  2  3  5  6  9  4  8  7   
1  2  3  4  5  6  9  8  7   
1  2  3  4  5  6  8  9  7   
1  2  3  4  5  6  7  8  9   
1  2  3  4  5  6  7  8  9
Copy after login

Algorithm performance/complexity
The incremental sequence of Hill sorting can be chosen arbitrarily, and the only condition required is the last One must be 1 (because it must be ordered by 1). However, different sequence selections will have a great impact on the performance of the algorithm. The code above demonstrates two increments.
Remember: It is best not to have a common factor other than 1 for every two elements in the incremental sequence! (Obviously, it doesn’t make much sense to sort a sequence ordered by 4 and then by 2).
The following are some common increment sequences.
The first increment is the increment originally proposed by Donald Shell, which is reduced by half until 1. According to research, using Hill increment, the time complexity is still O(n2).
The second increment Hibbard: {1, 3, ..., 2^k-1}. The time complexity of this incremental sequence is approximately O(n^1.5).
The third increment Sedgewick increment: (1, 5, 19, 41, 109,...), the generated sequence is either 9*4^i - 9*2^i + 1 or 4^ i - 3*2^i + 1.

Algorithm stability
We all know that insertion sort is a stable algorithm. However, Shell sort is a multiple insertion process. In one insertion, we can ensure that the order of the same elements is not moved, but in multiple insertions, the same elements may be moved in different insertion rounds, and finally the stability is destroyed. Therefore, Shell sorting is not stable. algorithm.

Applicable scenarios of the algorithm
Although Shell sorting is fast, it is insertion sorting after all, and its order of magnitude is not as fast as the rising star-quick sorting O(n㏒n). Shell sorting is not a good algorithm in the face of large amounts of data. However, it is perfectly fine for small to medium sized data.

For more detailed explanations of the Hill sorting algorithm and related Java code implementation related articles, please pay attention to the PHP Chinese website!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

See all articles