美文网首页
常见算法

常见算法

作者: JuneLeo | 来源:发表于2021-01-18 11:38 被阅读0次

单链表反转

public class NodeRevert {
    public static void main(String[] args) {
        Node node5 = new Node(5, null);
        Node node4 = new Node(4, node5);
        Node node3 = new Node(3, node4);
        Node node2 = new Node(2, node3);
        Node root = new Node(1, node2);

        Node temp = root;
        while (temp != null) {
            System.out.print(temp.a + ",");
            temp = temp.next;
        }
        /**
         * 单链表反转
         */
        temp = revertNode(root);

        while (temp != null) {
            System.out.print(temp.a + ",");
            temp = temp.next;
        }
    }

    /**
     * 反转单链表
     *
     * @param node
     * @return
     */
    public static Node revertNode(Node node) {
        Node per = null;
        while (node != null) {
            Node next = node.next;
            node.next = per;
            per = node;
            node = next;
        }
        return per;
    }

    static class Node {
        public Node(int a, Node next) {
            this.a = a;
            this.next = next;
        }

        public int a;
        public Node next;
    }
}

冒泡排序

public class BubbleSort {
    public static void main(String[] args) {
        /**
         * 冒泡排序 时间复杂度 O(n^2)
         */
        int[] bubbleArray = ArrayCode.ARRAY;
        long startTime = System.currentTimeMillis();
        bubbleSort(bubbleArray);
        System.out.println("bubbleArray time : " + (System.currentTimeMillis() - startTime));
        for (int i = 0; i < bubbleArray.length; i++) {
            System.out.print(bubbleArray[i] + ",");
        }
        System.out.println("--------");
    }
    /**
     * 冒泡排序
     *
     * @param array
     * @return
     */
    static int[] bubbleSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 1; j < array.length; j++) {
                if (array[j - 1] > array[j]) {
                    int temp = array[j - 1];
                    array[j - 1] = array[j];
                    array[j] = temp;
                }
            }
        }
        return array;
    }
}

选择排序

public class SelectSort {
    public static void main(String[] args) {
        /**
         * 选择排序
         */
        int[] selectArray = ArrayCode.ARRAY;
        long startTime = System.currentTimeMillis();
        selectSort(selectArray);
        System.out.println("selectArray time : " + (System.currentTimeMillis() - startTime));
        for (int i = 0; i < selectArray.length; i++) {
            System.out.print(selectArray[i] + ",");
        }
        System.out.println("--------");
    }

    /**
     * 选择排序
     *
     * @param array
     * @return
     */
    static int[] selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int index = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[index]) {
                    index = j;
                }
            }
            int temp = array[index];
            array[index] = array[i];
            array[i] = temp;
        }
        return array;
    }
}

插入排序

public class InsertSort {
    public static void main(String[] args) {
        /**
         * 插入排序
         */
        int[] insertArray = ArrayCode.ARRAY;
        long startTime = System.currentTimeMillis();
        insertSort(insertArray);
        System.out.println("insertArray time : " + (System.currentTimeMillis() - startTime));
        for (int i = 0; i < insertArray.length; i++) {
            System.out.print(insertArray[i] + ",");
        }
        System.out.println("--------");
    }

    /**
     * 插入排序
     *
     * @param array
     * @return
     */
    static int[] insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            for (int j = i; j >= 1; j--) {
                if (array[j] < array[j - 1]) {
                    int temp = array[j];
                    array[j] = array[j - 1];
                    array[j - 1] = temp;
                }
            }
        }
        return array;
    }
}

希尔排序

public class ShellSort {
    public static void main(String[] args) {
        /**
         * 希尔排序
         */
        int[] shellArray = ArrayCode.ARRAY;
        long startTime = System.currentTimeMillis();
        shelltSort(shellArray);
        System.out.println("shellArray time : " + (System.currentTimeMillis() - startTime));
        for (int i = 0; i < shellArray.length; i++) {
            System.out.print(shellArray[i] + ",");
        }
        System.out.println("--------");
    }

    /**
     * 希尔排序
     *
     * @param array
     * @return
     */
    static int[] shelltSort(int[] array) {
        for (int i = array.length / 2; i >= 1; i = i / 2) {
            for (int j = 0; j < i; j++) {
                //插入排序交换组内数据
                int index = j;
                while (index < array.length) {
                    int tempIndex = index;
                    while ((tempIndex - i) >= 0 && array[tempIndex - i] > array[tempIndex]) {
                        int temp = array[tempIndex - i];
                        array[tempIndex - i] = array[tempIndex];
                        array[tempIndex] = temp;
                        tempIndex = tempIndex - i;
                    }
                    index = index + i;
                }
            }
        }
        return array;
    }
}

快速排序

public class QuickSort {
    public static void main(String[] args) {
        /**
         * 快速排序
         */
        int[] quickArray = ArrayCode.ARRAY;
        long startTime = System.currentTimeMillis();
        quickSort(quickArray, 0, quickArray.length - 1);
        System.out.println("quickArray time : " + (System.currentTimeMillis() - startTime));
        for (int i = 0; i < quickArray.length; i++) {
            System.out.print(quickArray[i] + ",");
        }
        System.out.println("--------");
    }
    /**
     * 快排
     *
     * @param array
     * @param start
     * @param end
     * @return
     */
    static int[] quickSort(int[] array, int start, int end) {
        if (start < end) {
            int key = array[start];
            int i = start, j = end;
            while (i < j) {
                while (i < j && array[j] >= key) {
                    j--;
                }
                array[i] = array[j];
                while (i < j && array[i] <= key) {
                    i++;
                }
                array[j] = array[i];
            }
            array[i] = key;
            quickSort(array, start, i - 1);
            quickSort(array, i + 1, end);
        }
        return array;
    }
}

归并排序

public class MergeSort {
    public static void main(String[] args) {
        /**
         * 归并排序
         */
        int[] mergeArray = ArrayCode.ARRAY;
        long startTime = System.currentTimeMillis();
        mergeArray = mergeSort(mergeArray);
        System.out.println("mergeArray time : " + (System.currentTimeMillis() - startTime));
        for (int i = 0; i < mergeArray.length; i++) {
            System.out.print(mergeArray[i] + ",");
        }
        System.out.println("--------");
    }
    /**
     * 归并排序
     *
     * @param array
     * @return
     */
    static int[] mergeSort(int[] array) {
        if (array.length < 2) {
            return array;
        }
        int middle = array.length / 2;
        int[] left = Arrays.copyOfRange(array, 0, middle);
        int[] right = Arrays.copyOfRange(array, middle, array.length);
        return merge(mergeSort(left), mergeSort(right));
    }

    static int[] merge(int[] a, int[] b) {
        int[] array = new int[a.length + b.length];
        int i = 0, j = 0, index = 0;
        while (index < array.length && i < a.length && j < b.length) {
            if (a[i] < b[j]) {
                array[index++] = a[i++];
            } else {
                array[index++] = b[j++];
            }
        }
        while (i < a.length) {
            array[index++] = a[i++];
        }
        while (j < b.length) {
            array[index++] = b[j++];
        }
        return array;
    }
}

堆排序

public class HeapSort {
    public static void main(String[] args) {
        /**
         * 堆排序
         */
        int[] heapArray = ArrayCode.ARRAY;
        long startTime = System.currentTimeMillis();
        heapArray = maxHeapSort(heapArray);
        System.out.println("heapArray time : " + (System.currentTimeMillis() - startTime));
        for (int i = 0; i < heapArray.length; i++) {
            System.out.print(heapArray[i] + ",");
        }
    }

    /**
     * 堆排序
     *
     * @param array
     * @return
     */
    static int[] maxHeapSort(int[] array) {
        for (int i = array.length - 1; i >= 1; i--) {
            buildMaxHeap(array, 0, i);
            swap(array, 0, i);
        }
        return array;
    }

    private static void buildMaxHeap(int[] array, int start, int end) {
        for (int i = end / 2; i >= 0; i--) {
            heaply(array, i, end);
        }
    }

    private static void heaply(int[] array, int root, int end) {
        int left = root * 2 + 1;
        int right = root * 2 + 2;
        if (left <= end && array[left] > array[root]) {
            swap(array, left, root);
            if (right <= end && array[right] > array[root]) {
                swap(array, right, root);
            }
        } else if (right <= end && array[right] > array[root]) {
            swap(array, right, root);
            if (left <= end && array[left] > array[root]) {
                swap(array, left, root);
            }
        }
    }

    private static void swap(int[] array, int a, int b) {
        int temp = array[a];
        array[a] = array[b];
        array[b] = temp;
    }
}

二分查找

public class BinarySearch {
    public static void main(String[] args) {
        /**
         * 二分查找
         */
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        binarySearch(array, 0);
    }

    /**
     * 二分查找
     *
     * @param array
     * @param key
     * @return
     */
    static int binarySearch(int[] array, int key) {
        int start = 0;
        int end = array.length - 1;
        while (start <= end) {
            int middle = (start + end) / 2;
            if (array[middle] == key) {
                return middle;
            } else if (array[middle] > key) {
                end = middle - 1;
            } else {
                start = middle + 1;
            }
        }
        return -1;
    }
}

重建二叉树

public class RebuildTree {
    public static void main(String[] args) {

        /**
         * 前序遍历 preorder = [3,9,20,15,7]   根-左-右
         * 中序遍历 inorder = [9,3,15,20,7]    左-根-右
         */
        int[] preArray = {1, 2, 4, 7, 3, 5, 6, 8};
        int[] middleArray = {4, 7, 2, 1, 5, 3, 8, 6};
        Node node = new Node();
        buildBinaryTree(preArray, middleArray, node);
    }

    private static void buildBinaryTree(int[] preArray, int[] middleArray, Node root) {
        if (preArray.length <= 0 || middleArray.length <= 0) {
            return;
        }
        root.a = preArray[0];
        int middleIndex = 0;
        while (root.a != middleArray[middleIndex]) {
            middleIndex++;
        }
        if (middleIndex > 0) {
            int[] leftMiddleArray = Arrays.copyOfRange(middleArray, 0, middleIndex);
            int[] leftPerArray = Arrays.copyOfRange(preArray, 1, middleIndex + 1);
            Node node = new Node();
            root.per = node;
            buildBinaryTree(leftPerArray, leftMiddleArray, node);
        }

        if (middleIndex < middleArray.length - 1) {
            int[] rightMiddleArray = Arrays.copyOfRange(middleArray, middleIndex + 1, middleArray.length);
            int[] rightPerArray = Arrays.copyOfRange(preArray, middleIndex + 1, middleArray.length);
            Node node = new Node();
            root.next = node;
            buildBinaryTree(rightPerArray, rightMiddleArray, node);
        }
    }

    static class Node {
        public int a;
        public Node per;
        public Node next;
    }
}

相关文章

  • 数据结构与算法

    常见排序算法 堆排序 算法大全 算法大汇总

  • 算法——常见算法

    记录算法,三篇文章,持续更新,文章本意只是为了方便本人日后查看,如需转载请注明出处 算法——常见算法记录[http...

  • 缓存淘汰算法

    常见算法:LRULRU-K2QMQ 缓存淘汰算法

  • 排序算法

    常见的排序算法 常见的排序算法有:插入、希尔、选择、冒泡、归并、快速、堆排序。。。 插入排序 算法步骤一、从数组的...

  • 常见排序算法

    整理常见排序算法。

  • Rust数据结构——排序算法(一)

    Rust数据结构——排序算法(一) 0x01 常见的排序算法 排序算法是数据结构中很常见的算法。如果你了解过数据结...

  • 常见算法

    1. 组合数字 2. 冒泡排序 简介:最基础的排序 原理. 过程跟踪 内部:相邻元素两两比较 数次比较循环...

  • 常见算法

    1. 将两个有序数组合成为一个有序数组

  • 常见算法

    十种常见排序算法可以分为两大类: 非线性时间比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O...

  • 常见算法

    1.冒泡排序 比较相邻两个数的大小,符合条件,交换位置 调用函数 2.选择排序 前一个数依次和后面每一个数比较,满...

网友评论

      本文标题:常见算法

      本文链接:https://www.haomeiwen.com/subject/geuzaktx.html