Java basic algorithm of Hill sorting (Shell Sort)

1. Algorithm introduction

  1. Divide the array of n data into n / 2 groups (use n / 2 as the increment, if it is an integer multiple of the increment, it is a group). For example, [117, 101, 106, 155, 112, 60] is divided into 3 groups, namely [117, 155], [101, 112], [106, 60].
  2. Then insert and sort each group.
  3. Then divide the n data into n / 2*2 groups, and then sort by group.
  4. If the increment is 1 (n data is a group), then exit after sorting.

2. Example demonstration

Original array: [117, 101, 106, 155, 112, 60, 90, 110]

Length: 8

First grouping:Divided into 4 groups (8/2)

Group 1: [117, 112]
Group 2: [101, 60]
Group 3: [106, 90]
Group 4: [155, 110]

Then insert sort (sort from small to large) for each group.

Group 1: [112, 117]
Group 2: [60, 101]
Group 3: [90, 106]
Group 4: [110, 155]

The original array after sorting is: [112, 60, 90, 110, 117, 101, 106, 155]

Second grouping: Divide into 2 groups (8 / 2 * 2)

Group 1: [112, 90, 117, 106]
Group 2: [60, 110, 101, 155]

Then insert sort (sort from small to large) for each group.

Group 1: [90, 106, 112, 117]
Group 2: [60, 101, 110, 155]

The original array after sorting is: [90, 60, 106, 101, 112, 110, 117, 155]

The third grouping: divided into 1 group (8 / 2 * 2 * 2)

Group 1: [90,60,106,101,112,110,117,155]

Insert the sort directly, and exit the loop after sorting.

Sorted array: [60, 90, 101, 106, 110, 112, 117, 155]

3. Diagram

Sorting diagram

4. Code implementation (exchange method)

package sort;

import java.util.Arrays;

/**
 * <p>
 *
 * </p>
 *
 * @author: D
 * @since: 2020/9/9
 * @version: 1
 */
public class ShellSortDemo {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    117, 101, 106, 155, 112, 60, 90, 110};
        shellSort(arr);
        System.out.println("排序后的结果: " + Arrays.toString(arr));
    }

    private static void shellSort(int[] arr) {
    
    
        int temp;
        for (int len = arr.length / 2; len > 0; len /= 2) {
    
    
            System.out.println("分为:" + len + "组");
            System.out.println("跨度:" + len + "为一组");
            for (int i = 0; i < len; i++) {
    
    
                int k = i;
                System.out.print("第" + (i + 1) + "组 :");
                System.out.print("[");
                while (k < arr.length) {
    
    
                    System.out.print(arr[k] + ",");
                    k += len;
                }
                System.out.println("]");

            }
            System.out.println("----------------------");
            System.out.println("比较次数:" + (arr.length - len));
            for (int i = len; i < arr.length; i++) {
    
    
                //交换法
                System.out.println("交换前 = " + Arrays.toString(arr));
                for (int j = i - len; j >= 0; j -= len) {
    
    
                    System.out.println("比较下标" + j + "的数据:" + arr[j] + ", 和下标" + (j + len) + "的数据:" + arr[j + len] );
                    if (arr[j] > arr[j + len]) {
    
    
                        temp = arr[j];
                        arr[j] = arr[j + len];
                        arr[j + len] = temp;
                        System.out.println("进行交换");
                    }else {
    
    
                        System.out.println("前面已经是更小的了,不交换");
                    }
                }
                System.out.println("交换后 = " + Arrays.toString(arr));
                System.out.println();
            }
        }
    }
}

5. Code implementation (shift method)

package sort;

import java.util.Arrays;

/**
 * <p>
 *
 * </p>
 *
 * @author: D
 * @since: 2020/9/9
 * @version: 1
 */
public class ShellSortDemo {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    117, 101, 106, 155, 112, 60, 90, 110};
        System.out.println("原数组: " + Arrays.toString(arr));
        shellSort(arr);
        System.out.println("排序后的结果: " + Arrays.toString(arr));
    }

    private static void shellSort(int[] arr) {
    
    
        int temp;
        int index;
        for (int len = arr.length / 2; len > 0; len /= 2) {
    
    
            System.out.println("分为:" + len + "组");
            System.out.println("跨度:" + len + "为一组");
            for (int i = 0; i < len; i++) {
    
    
                int k = i;
                System.out.print("第" + (i + 1) + "组 :");
                System.out.print("[");
                while (k < arr.length) {
    
    
                    System.out.print(arr[k] + ",");
                    k += len;
                }
                System.out.println("]");

            }
            System.out.println("----------------------");
            for (int i = len; i < arr.length; i++) {
    
    
                System.out.println("移位前 = " + Arrays.toString(arr));
                index = i;
                temp = arr[index];
                System.out.println("比较下标" + (index  - len ) + "的数据:" + arr[index - len] + ", 和下标" + (index) + "的数据:" + arr[index] );
                if (arr[index] < arr[index - len]) {
    
    
                    while (index - len >= 0 && temp < arr[index - len]) {
    
    
                        arr[index] = arr[index - len];
                        index -= len;
                    }
                    arr[index] = temp;
                }
                System.out.println("移位后 = " + Arrays.toString(arr));
                System.out.println();
            }
        }
    }
}

6. Operation result (exchange method)

image-20200909113712543

image-20200909113800975

image-20200909113826814

image-20200909114323165

image-20200909114338985

image-20200909114348254

7. Other sorting

Selection Sort (Selection Sort)
Insertion Sort (Insertion Sort)
Bubble Sort (Bubble Sort)
Quick Sort (Quick Sort)
Heap Sort (Heap Sort)

Guess you like

Origin blog.csdn.net/d875708765/article/details/108504259