常见算法

单链表反转

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;
    }
}

你可能感兴趣的:(常见算法)