Quick sort, Hill sort, merge sort (detailed explanation)

Table of contents

1. Quick sort:

2. Illustration 

3. Code

2. Hill sorting

1. Steps

2. Examples

3. Code 

 4. Algorithm analysis

3. Merge sort

1. Definition

2. Algorithm ideas

3. Code

4. Algorithm analysis 


1. Quick sort :

It is a divide and conquer idea. Divide the sequence that needs to be sorted into small sequences, and then divide them layer by layer until each sequence has only one number. In each division, at least one number, that is, the position of the base number, can be determined. Finally, Complete the sorting. The following sorting is from small to large.

2. Illustration 

3. Code
package com.kfm.it.Array;

import java.util.Arrays;

public class Arrayquilk {

    public static void main(String[] args) {
        int[] arr = {2, 1, 3, 4, 5, 6};

        quilk(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr));
    }

    public static void quilk(int[] arr, int start, int end) {
        if (start>=end){
            return;
        }
        int base = arr[start];
        int left = start;
        int right = end;

        while (left<right){
            while (left<right && base<=arr[right]){
                right--;
            }
            while (left<right && base>=arr[left]){
                left++;
            }
            int temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
        }
        arr[start] = arr[left];
        arr[left] = base;

        quilk(arr,start,left-1);
        quilk(arr,left+1,end);
    }
}

2. Hill sorting

The Hill sort is named after its designer, Hill. Hill sort is also an insertion sort, which is a more efficient version of insertion sort.

1. Steps

(1) First select an integer gap (gap means interval, gap) as the increment. Generally, length/2 is used as the increment, and then group all the elements with a distance of gap into a group, and divide each element into a group. insertion sort

(2) Reduce the increment, let gap = gap/2 and then repeat step 1 until gap equals 1.

2. Examples

There is an array as follows, use Hill to sort the array

 

3. Code 
package com.kfm.it.Rank;

import java.util.Arrays;

public class Shellsort {
    public static void main(String[] args) {
        int [] arr = {2,5,2,3,5,6};
        int[] ints = shellSort(arr);
        System.out.println(Arrays.toString(ints));
    }
    public static int[] shellSort(int[] arr) {
        for (int gap = arr.length / 2; gap > 0; gap /= 2) {
            for (int i = gap; i < arr.length; i++) {
                for (int j = i - gap; j >= 0; j -= gap) {
                    if (arr[j] > arr[j + gap]) {
                        int temp = arr[j];
                        arr[j] = arr[j + gap];
                        arr[j + gap] = temp;
                    }
                }
            }
        }
        return arr;
    }
}
 4. Algorithm analysis

Stability: Unstable

Time complexity: Optimal: O(nlog²n)

Worst: O(nlog²n)

Average: O(nlogn)

Space complexity: O(1)

3. Merge sort

In 1945, John von Neumann invented merge sort, which is a typical application of the divide-and-conquer algorithm.

1. Definition


Merge sort is an effective sorting algorithm based on merge operations. This algorithm is a very typical application of the divide and conquer method. Merge the already ordered subsequences to obtain a completely ordered sequence; that is, first make each subsequence orderly, and then make the subsequence segments orderly.

2. Algorithm ideas


The merge sort algorithm has two basic operations. One is dividing, which is the process of dividing the original array into two sub-arrays. The other is Zig, which merges two sorted arrays into a larger sorted array.

The linear list to be sorted is continuously divided into several sub-lists until each sub-list contains only one element. At this time, the sub-list containing only one element can be considered as an ordered list.
Merge the sub-tables in pairs. Each time they are merged, a new and longer ordered list will be generated. Repeat this step until there is only one sub-table left, which is the sorted linear list.

3. Code
package com.kfm.it.Rank;

import java.util.Arrays;

public class Dui {
    public static void main(String[] args) {
        int[] array = {8,4,5,7,1,3,6,2};
        int[] ints = MergeSort(array);
        System.out.println(Arrays.toString(ints));
    }
    public static int[] MergeSort(int[] array) {
        if (array.length < 2) return array;
        int mid = array.length / 2;
        int[] left = Arrays.copyOfRange(array, 0, mid);
        int[] right = Arrays.copyOfRange(array, mid, array.length);
        return merge(MergeSort(left), MergeSort(right));

    }
    public static int[] merge(int[] left, int[] right) {
        int[] result = new int[left.length + right.length];
        for (int index = 0, i = 0, j = 0; index < result.length; index++) {
            if (i >= left.length)
                result[index] = right[j++];
            else if (j >= right.length)
                result[index] = left[i++];
            else if (left[i] > right[j])
                result[index] = right[j++];
            else
                result[index] = left[i++];
        }
        return result;
    }
}
4. Algorithm analysis 

Algorithm performance: second only to quick sort in speed.

Time complexity: O(nlogn) .

Space complexity: O(N) , merge sort requires an array of the same length as the original array as an auxiliary sort.

Stability: Stable .

Guess you like

Origin blog.csdn.net/pachupingminku/article/details/132452058