经典排序算法

2019-07-25  本文已影响0人  Jimhou
冒泡排序
 public static void bubbleSort(int[] a) {
        boolean swap = false;
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a.length - i - 1; j++) {
                if (a[j] > a[j + 1]) {
                    swap = true;
                    int t = a[j + 1];
                    a[j + 1] = a[j];
                    a[j] = t;
                }
            }
            if (!swap) break;
        }
    }
插入排序法
 public static void insertionSort(int[] a) {
        for (int i = 1; i < a.length; i++) {
            int j = i - 1;
            int target = a[i];
            for (; j >= 0; j--) {
                if (a[j] > target) {
                    a[j + 1] = a[j];
                } else {
                    break;
                }
            }
            a[j + 1] = target;
        }
    }
选择排序法
 public static void selectionSort(int[] a) {
        for (int i = 0; i < a.length; i++) {
            int minValueIndex = i;
            int minV = a[i];
            for (int j = i; j < a.length; j++) {

                if (a[j] < minV) {
                    minValueIndex = j;
                    minV = a[j];
                }
            }
            int t = a[i];
            a[i] = a[minValueIndex];
            a[minValueIndex] = t;
        }
    }
归并排序算法
mergeSort(a,0,r) = merge(mergeSort(a,0,q), mergeSort(a,q+1,r))
终止条件:
p >= r 不用再继续分解
public static void mergeSort(int[] a, int startIndex, int endIndex) {
        if (startIndex >= endIndex) {
            return;
        }
        int mid = (startIndex + endIndex) / 2;

        mergeSort(a, startIndex, mid);
        mergeSort(a, mid + 1, endIndex);
        merge(a, startIndex, mid, endIndex);
   }
    private static void merge(int[] a, int startIndex, int mid, int endIndex) {
        int i = startIndex, j = mid + 1;
        if (i >= j) {
            return;
        }
        int[] tmp = new int[endIndex - startIndex + 1];
        int k = 0;
        while (i <= mid && j <= endIndex) {

            if (a[i] <= a[j]) {
                tmp[k++] = a[i++];
            } else {
                tmp[k++] = a[j++];
            }
        }

        if (i <= mid || j <= endIndex) {
            int start = i <= mid ? i : j;
            int end = i <= mid ? mid : endIndex;
            for (; start <= end; start++) {
                tmp[k++] = a[start];
            }

        }
        for (int m = 0; m < tmp.length; m++) {
            a[m + startIndex] = tmp[m];
        }
    }
快排
public static void quickSort(int[] a, int p, int r) {

        if (p >= r) {
            return;
        }
        int q = partition(a, p, r);
        quickSort(a, p, q - 1);
        quickSort(a, q + 1, r);
    }

    private static int partition(int[] a, int p, int r) {

        int pivot = a[r];
        int i = p;
        int j = p;
        for (; j < r; j++) {
            if (a[j] > pivot) {
                int tmp = a[j];
                a[j] = a[i];
                a[i] = tmp;
                i++;
            }
        }
        int tmp = a[i];
        a[i] = a[r];
        a[r] = tmp;
        return i;
    }
堆排序

堆有以下两个特性:

使用堆排序有两个过程,建堆+排序

public class Heap {
    private int[] a; // 数组,从下标 1 开始存储数据
    private int n;  // 堆可以存储的最大数据个数
    private int count; // 堆中已经存储的数据个数
 public Heap(int capacity) {
        a = new int[capacity + 1];
        n = capacity;
        count = 0;
    }

 private void buildHeap(int[] a, int n) {
        for (int i = n / 2; i >= 1; --i) {
            heapify(a, n, i);
        }
    }
 private void heapify(int[] a, int n, int i) { // 自上往下堆化
        while (true) {
            int maxPos = i;
            if (i * 2 <= n && a[i] < a[i * 2]) maxPos = i * 2;
            if (i * 2 + 1 <= n && a[maxPos] < a[i * 2 + 1]) maxPos = i * 2 + 1;
            if (maxPos == i) break;
            swap(a, i, maxPos);
            i = maxPos;
        }
    }

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

    // n 表示数据的个数,数组 a 中的数据从下标 1 到 n 的位置。
    public void sort(int[] a, int n) {
        buildHeap(a, n);
        int k = n;
        while (k > 1) {
            swap(a, 1, k);
            --k;
            heapify(a, k, 1);
        }
    }
上一篇下一篇

猜你喜欢

热点阅读