基础算法

排序算法

(1) 快速排序(quick sort)

public static void quickSort(int[] array, int left, int right) {
        if (array == null || array.length == 0) return;
        if (left > right) return;
        int key = array[left];
        int l = left;
        int r = right;
        while (l != r) {
            while (array[r] >= key && l < r) {
                r--;
            }
            while (array[l] <= key && l < r) {
                l++;
            }
            if (l < r) {
                int tmp = array[l];
                array[l] = array[r];
                array[r] = tmp;
            }
        }
        array[left] = array[l];
        array[l] = key;
        quickSort1(array, left, l - 1);
        quickSort1(array, l + 1, right);
    }
}

(2) 冒泡排序

public static void bubbleSort(int[] array){
        int i,j,tmp;
        boolean flag;
        for (i=0;i< array.length-1;i++){
            for (j=0,flag=true;j<array.length-1-i;j++){
                if (array[j]>array[j+1]){
                    tmp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=tmp;
                    flag=false;
                }
                if (flag==true) continue;
            }
        }
    }

(3) 选择排序

public static void selectSort(int[] array) {
        int i, j, min, tmp;
        for (i = 0; i < array.length - 1; i++) {
            for (min = i, j = i + 1; j < array.length; j++) {
                if (array[min] > array[j]) {
                    min = j;
                }
            }
            tmp = array[i];
            array[i] = array[min];
            array[min] = tmp;
        }
    }

(4) 插入排序

public static void insertSort(int[] array) {
        int tmp, i, j;
        for (i = 1; i < 10; i++) {
            if (array[i] < array[i - 1]) {
                tmp = array[i];
                for (j = i - 1; j >= 0 && array[j] > tmp; j--) {
                    array[j + 1] = array[j];
                }
                array[j + 1] = tmp;
            }
        }
    }

(5) 堆排序

public static void swap(int[] array, int node1, int node2) {
        int tmp;
        tmp = array[node1];
        array[node1] = array[node2];
        array[node2] = tmp;
    }
    
    public static void heapify(int[] tree, int n, int i) {
        if (i >= n) {
            return;
        }
        int c1 = i * 2 + 1;
        int c2 = i * 2 + 2;
        int max = i;
        if (c1 < n && tree[c1] > tree[max]) {
            max = c1;
        }
        if (c2 < n && tree[c2] > tree[max]) {
            max = c2;
        }
        if (max != i) {
            swap(tree, max, i);
            heapify(tree, n, max);
        }
    }

    public static void build_heap(int[] tree, int n) {
        int last_node = n - 1;
        int parent = (last_node - 1) / 2;
        int i;
        for (i = parent; i >= 0; i--) {
            heapify(tree, n, i);
        }
    }

    public static void heapSort(int[] tree) {
        int n = tree.length;
        build_heap(tree, n);
        int i;
        for (i = n - 1; i >= 0; i--) {
            swap(tree, i, 0);
            heapify(tree, i, 0);
        }
    }

(6) 归并排序

讲解视频:https://www.bilibili.com/video/BV1ZC4y1872h?p=2&spm_id_from=pageDriver

public class MergeSortDemo {
    public static void mergeSort(int[] array, int low, int high) {
        if (low == high) {
            return;
        }
        int mid = (low + high) >>> 1;
        mergeSort(array, low, mid);
        mergeSort(array, mid + 1, high);
        merge(array, low, mid, high);//合并
    }

    private static void merge(int[] array, int low, int mid, int high) {
        int s1 = low;
        int s2 = mid + 1;
        int[] tmp = new int[high - low + 1];//临时数组
        int i = 0; //临时数组tmp的下标
        while (s1 <= mid && s2 <= high) {//说明有数据
            if (array[s1] < array[s2]) {
                tmp[i++] = array[s1++];
            } else {
                tmp[i++] = array[s2++];
            }
        }
        while (s1 <= mid) {
            tmp[i++] = array[s1++];
        }
        while (s2 <= high) {
            tmp[i++] = array[s2++];
        }
        System.out.println(tmp.length);//2 2 4 2 2 4
        for (int j = 0; j < tmp.length; j++) {//复制数组,需注意起始下标加上low,否则每次都是从0号下标开始
            array[j + low] = tmp[j];
        }
    }

    public static void main(String[] args) {
        int[] array = {3, 6, 1, 4, 8, 0, 2, 5};
        mergeSort(array, 0, array.length - 1);
        System.out.println(Arrays.toString(array));
    }
}
复杂度一览
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一个算法只是一个把确定的数据结构的输入转化为一个确定的数据结构的输出的function。算法内在的逻辑决定了如何转...
    浅浅而谈阅读 2,361评论 0 0
  • 一个算法只是一个把确定的数据结构的输入转化为一个确定的数据结构的输出的function。算法内在的逻辑决定了如何转...
    浅浅而谈阅读 1,163评论 0 0
  • 递归 简而言之,就是自己调自己。 当满足如下条件时,则可用递归来解决: 一个问题的解可以分解为几个子问题的解 这个...
    退而结网007阅读 2,435评论 0 0
  • 需求:分别用 冒泡排序法,快速排序法,选择排序法,插入排序法将下面数组中 的值按照从小到的顺序进行排序。 1、冒泡...
    Gamer_young阅读 4,865评论 1 2
  • 算法分类 十种常见排序算法可以分为两大类: 非线性时间比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度...
    南山码僧阅读 5,694评论 0 2