常见排序算法总结

排序算法

综述

本文主要是整理几种常见的排序算法(选择排序,插入排序,希尔排序,归并排序、快速排序,堆排序)源码Github地址

排序时需要的方法

package org.jpractice.algorithm.sort;

/** * @author: 作者: xuefei * @date: 创建时间:2019-02-23 17:54:44 * @Description: 排序算法需要的公共方法 * @version V1.0 */
public class AbstractSort {

    /** * 判断两个变量的大小 * * @param v * @param w * @return */
    public static boolean less(Comparable v, Comparable w) {
        return v.compareTo(w) < 0;
    }

    /** * 交换指定的两个变量 * * @param a * @param i * @param j */
    public static void exch(Comparable[] a, int i, int j) {
        Comparable t = a[i];
        a[i] = a[j];
        a[j] = t;
    }

    /** * 判断集合是否已经排序 * * @param a * @return */
    public static boolean isSorted(Comparable[] a) {
        for (int i = 1; i < a.length; i++) {
            if (less(a[i], a[i - 1])) {
                return false;
            }
        }
        return true;
    }

    /** * 用来展示排序的结果 * * @param a */
    public static void show(Comparable[] a) {
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println("");
    }
}

选择排序

package org.jpractice.algorithm.sort;

/** * @author: 作者: xuefei * @date: 创建时间:2018-08-14 06:47:16 * @Description: 选择排序算法 * @version V1.0 */
public class SelectionSort extends AbstractSort {
    
    public static void sort(Comparable[] a) {
        int length = a.length;
        int min = 0;
        for (int i = 0; i < length; i++) {
            for (int j = i + 1; j < length; j++) {
                if (less(a[j], a[min])) {
                    min = j;
                }
                System.out.println(min);
                exch(a, min, i);
            }
        }
        show(a);
    }

    public static void main(String[] args) {
        Integer[] numbsers = new Integer[] { 5, 3, 7, 11, 22, 4, 3 };
        sort(numbsers);
    }

}

插入排序

package org.jpractice.algorithm.sort;

/** * @author: 作者: xuefei * @date: 创建时间:2018-08-16 17:32:43 * @Description: 插入排序 * @version V1.0 */
public class InsertionSort extends AbstractSort{
    
    public static void sort(Comparable[] a) {
        int length = a.length;
        for (int i = 0; i < length - 1; i++) {
            for (int j = i + 1; j >= 1; j--) {
                if (less(a[j], a[j - 1])) {
                    exch(a, j, j - 1);
                }
            }
        }

        show(a);
    }
    
    public static void main(String[] args) {
        Integer[] aa = { 11, 9, 8, 7, 3, 9, 2 };
        sort(aa);
    }

}

希尔排序

package org.jpractice.algorithm.sort;

/** * @author: 作者: xuefei * @date: 创建时间:2018-08-23 08:20:35 * @Description: 希尔排序 * @version V1.0 */
public class ShellSort extends AbstractSort {

    public void sort(Comparable[] a) {

        int length = a.length;
        int h = 1;
        while (h < length / 3) {
            h = 3 * h + 1;
        }
        
        while (h >= 1) {
            for (int i = h; i < length; i++) {
                for (int j = i; j >= h && less(a[j], a[j - h]); j -= h) {
                    exch(a, j - h, j);
                }
            }
            h = h / 3;
        }
        show(a);
    }

    public static void main(String[] args) {
        Integer[] arrays = { 1, 3, 6, 2, 13, 7, 9, 2, 1 };
        ShellSort shellSort = new ShellSort();
        shellSort.sort(arrays);

    }
}

归并排序

/** * */
package org.jpractice.algorithm.sort;

/** * @author: 作者: xuefei * @date: 创建时间:2018-12-18 13:12:01 * @Description: TODO * @version V1.0 */
public class MergeSort extends AbstractSort {

    public void sort(Comparable[] a) {
        int length = a.length;
        sort(a, 0, length - 1);
    }

    private void sort(Comparable[] a, int low, int high) {
        if (low < high) {
            int mid = (low + high) / 2;
            sort(a, low, mid);
            sort(a, mid + 1, high);
            merge(a, low, mid, high);
        }
    }

    private void merge(Comparable[] arr, int low, int mid, int high) {

        System.out.println(low + "-" + mid + "-" + high);
        show(arr);

        Comparable[] aux = new Comparable[arr.length];

        // Find sizes of two subarrays to be merged
        int n1 = mid - low + 1;
        int n2 = high - mid;

        /* Create temp arrays */
        Comparable L[] = new Comparable[n1];
        Comparable R[] = new Comparable[n2];

        /* Copy data to temp arrays */
        for (int i = 0; i < n1; ++i)
            L[i] = arr[low + i];
        for (int j = 0; j < n2; ++j)
            R[j] = arr[mid + 1 + j];

        /* Merge the temp arrays */

        // Initial indexes of first and second subarrays
        int i = 0, j = 0;

        // Initial index of merged subarry array
        int k = low;
        while (i < n1 && j < n2) {
            if (less(L[i], R[j])) {
                arr[k] = L[i];
                i++;
            } else {
                arr[k] = R[j];
                j++;
            }
            k++;
        }

        /* Copy remaining elements of L[] if any */
        while (i < n1) {
            arr[k] = L[i];
            i++;
            k++;
        }

        /* Copy remaining elements of R[] if any */
        while (j < n2) {
            arr[k] = R[j];
            j++;
            k++;
        }

    }

    // Driver method
    public static void main(String args[]) {
        String arr[] = { "M", "E", "R", "G", "E", "S", "O", "R", "T", "E", "X", "A", "M", "P", "L", "E" };
        // String arr[] = { "M", "E", "R", "G", "E", "S" };
        System.out.println("Given Array");
        show(arr);

        MergeSort ob = new MergeSort();
        ob.sort(arr);

        System.out.println("Sorted array");
        // printArray(arr);
    }
}

快速排序

/** * */
package org.jpractice.algorithm.sort;

import edu.princeton.cs.algs4.StdRandom;

/** * @author: 作者: xuefei * @date: 创建时间:2019-02-04 18:05:14 * @Description: 快速排序 * @version V1.0 */
public class QuickSort {

    private static int partition(Comparable[] a, int lo, int hi) {

        int i = lo, j = hi + 1;
        while (true) {
            while (less(a[++i], a[lo])) {
                if (i == hi) {
                    break;
                }
            }

            while (less(a[lo], a[--j])) {
                if (j == lo) {
                    break;
                }
            }

            if (i >= j) {
                break;
            }
            exch(a, i, j);
        }

        exch(a, lo, j);
        return j;
    }

    public static void sort(Comparable[] a) {
        StdRandom.shuffle(a);
        // print(a);
        sort(a, 0, a.length - 1);
    }

    private static void print(Comparable[] a) {
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println("");
    }

    public static void sort(Comparable[] a, int lo, int hi) {
        if (hi <= lo) {
            return;
        }
        int j = partition(a, lo, hi);
        sort(a, lo, j - 1);
        sort(a, j + 1, hi);
    }

    private static boolean less(Comparable v, Comparable w) {
        return v.compareTo(w) < 0;
    }

    private static void exch(Comparable[] a, int i, int j) {
        Comparable t = a[i];
        a[i] = a[j];
        a[j] = t;
    }

    public static void main(String[] args) {
        String[] a = new String[] { "Q", "U", "I", "C", "K", "S", "O", "R", "T", "E", "X", "A", "M", "P", "L", "E" };
        QuickSort.sort(a);
        print(a);
    }
}

堆排序

/** * */
package org.jpractice.algorithm.sort;

/** * @author: 作者: xuefei * @date: 创建时间:2019-02-15 09:10:14 * @Description: 堆排序 * @version V1.0 */
public class HeapSort {

    public static void main(String[] args) {
        String[] a = { "1", "4", "2", "66", "22", "45" };
        sort(a);
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }

    public static void sort(Comparable[] a) {
        int n = a.length;
        for (int k = n / 2; k >= 1; k--)
            sink(a, k, n);
        while (n > 1) {
            exch(a, 1, n);
            sink(a, 1, --n);
        }
    }

    private static void sink(Comparable[] pq, int k, int n) {
        while (2 * k <= n) {
            int j = 2 * k;
            if (j < n && less(pq, j, j + 1))
                j++;
            if (!less(pq, k, j))
                break;
            exch(pq, k, j);
            k = j;
        }
    }

    private static boolean less(Comparable[] a, int i, int m) {
        return a[i - 1].compareTo(a[m - 1]) < 0;
    }

    private static void exch(Object[] a, int i, int m) {
        Object swap = a[i - 1];
        a[i - 1] = a[m - 1];
        a[m - 1] = swap;
    }
}

点赞