Several common sorting algorithms

package com.rails;

import java.util.Arrays;

/**
 * @author: noob
 * @description : Bubble sort, selection sort, insertion sort (similar to bubble sort), quick sort, radix sort
 * @Date : 17:36 2018/04/19
 */

public class sort {
    public static void main(String[] args) {
        Integer[] s = {1, 6, 4, 34, 32, 45, 43, 26, 17};
        s = CockTailSort(s);
        System.out.print(Arrays.toString(s));
    }

    /// <summary>
    /// Cocktail sorting
    static Integer[] CockTailSort(Integer[] list) {
        // Determine if it has been sorted
        Boolean isSorted = false;
        //Because it is a two-way comparison, the number of comparisons is 1/2 of the original array.
        for (Integer i = 1; i <= list.length / 2; i++) {
            //sort from front to back (ascending)
            /**The activity space of the bubble sort is reduced from one side to the other by the primary key, and the activity return here is reduced from both sides to the middle*/
            for (Integer m = i - 1; m < list.length - i; m++) {
                //If the front is greater than the back, swap
                if (list[m] > list[m + 1]) {
                    Integer temp = list[m];
                    list[m] = list[m + 1];
                    list[m + 1] = temp;
                    isSorted = true;
                }
            }
            //sort from back to front (descending order)
            for (Integer n = list.length - i - 1; n > i; n--) {
                //If the front is greater than the back, swap
                if (list[n - 1] > list[n]) {
                    Integer temp = list[n];
                    list[n] = list[n - 1];
                    list[n - 1] = temp;
                    isSorted = true;
                }
            }
            //When there is no more sorting, exit early
            if (! isSorted)
                System.out.println(Arrays.toString(list));
            break;
        }
        return list;
    }

    public static Integer[] insert_sort(Integer array[]) {
        Integer temp;
        for (Integer i = 0; i < array.length - 1; i++) {
            for (Integer j = i + 1; j > 0; j--) {
                if (array[j] < array[j - 1]) {
                    temp = array[j - 1];
                    array[j - 1] = array[j];
                    array[j] = temp;
                } else {
                    break;
                }
            }
        }
        return array;
    }

    /**
     * two
     * Selection sort.
     */
    public static Integer[] select_sort(Integer array[], Integer lenth) {
        for (Integer i = 0; i < lenth - 1; i++) {
            Integer minIndex = i;
            for (Integer j = i + 1; j < lenth; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            if (minIndex != i) {
                Integer temp = array[i];
                array[i] = array[minIndex];
                array[minIndex] = temp;
            }
        }
        return array;
    }

    /**
     * Selection sort improvements
     */
    public static Integer[] Bselect_sort(Integer array[]) {
        for (Integer i = 0; i < array.length - 1; i++) {
            Integer temp = array[i];
            for (Integer j = i + 1; j < array.length; j++) {
                if (array[j] < temp) {
                    array[i] = array[j];
                    array[j] = temp;
                    temp = array[i];
                }
            }
        }
        return array;
    }

    /**
     * one,
     * Bubble sort pairwise comparison loop n times to sort the array
     * Optimization: Set the flag bit to determine whether the data has been sorted.
     */
    public static Integer[] BubbleSort(Integer[] arr) {
        Integer temp;//Temporary variable
        boolean flag = true;
        for (Integer i = 0; i < arr.length - 1; i++) { //Indicates the number of times, a total of arr.length-1 times.
            flag = true;
            for (Integer j = arr.length - 1; j > i; j--) {//j>i is used here because the first bubbling has found the smallest number, and the control will not exceed the bounds of the array
                if (arr[j] < arr[j - 1]) {
                    temp = arr[j];
                    arr[j] = arr[j - 1];
                    arr[j - 1] = temp;
                    flag = false;
                }
                if (flag) {
                    break;
                }
            }
        }
        return arr;
    }

    /**
     * Retrofit bubble sort
     */
    public static Integer[] SBubbleSort(Integer[] arr) {
        Integer temp;//Temporary variable
        for (Integer i = 0; i < arr.length - 1; i++) { //Indicates the number of times, a total of arr.length-1 times.
            for (Integer j = 0; j < arr.length - i - 1; j++) {//
                if (i == 0) {
                    System.out.println(j);
                }
                if (arr[j] > arr[j + 1]) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        return arr;
    }

    /**
     * Insertion sort inserts the data to be sorted into a new array one by one
     */
    static Integer [] sier_sort (Integer [] unsorted) {
        Integer[] temp = new Integer[unsorted.length];
        temp[0] = unsorted[0];//Copy the first data directly to the new array
        /**Control the first loop, starting from 1, looping to extract data*/
        for (Integer a = 1; a < unsorted.length; a++) {
            /**Compare the data extracted from the original array with the data of the new array until it encounters a number larger than him, and insert the data into the new array*/
            for (Integer v = 0; v < temp.length; v++) {
                if (temp[v] != null && unsorted[a] < temp[v]) {
                    temp = insertArray(temp, v, unsorted[a]);
                    break;
                } else if (temp[v] == null) {
                    temp = insertArray(temp, v, unsorted[a]);
                    break;
                }
            }

        }
        return temp;
    }

    /**
     * Hill sort uses insert a number into the specified position of the original array and returns the new array
     */
    static Integer[] insertArray(Integer[] begin, Integer index, Integer num) {
        Integer[] end = new Integer[begin.length];
        if (begin.length - 1 >= index) {
            for (Integer i = 0; i < end.length; i++)
                if (i < index) {
                    end[i] = begin[i];
                } else if (i == index) {
                    end[index] = num;
                } else {
                    end[i] = begin[i - 1];
                }
        }
        return end;
    }

    /**
     * Goblin sort
     */
    static Integer[] gnome_sort(Integer[] unsorted) {
        Integer i = 0;
        while (i < unsorted.length - 1) {
            if (unsorted[i] <= unsorted[i + 1]) {
                i++;
            } else {
                Integer tmp = unsorted[i];
                unsorted[i] = unsorted[i + 1];
                unsorted[i + 1] = tmp;
                i--;
            }
        }
        return unsorted;
    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324609427&siteId=291194637